MATLAB: How to find the indexes of an specific group of values in an array.

arraygroup find valuesMATLAB

Hi, guys. I have an array of data [ 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1,… ]
I need to find the first index where values equal to a group, for example, [0, 0, 0, 0]. (4 zeros in sequence).
In this case it will be position 10 11 12 13. In fact I just need to find the first index of the vector (i.e. 10).
Please, help! Thanks, Leo.

Best Answer

  • This looks strange but it works:
    data = [1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1];
    Idx = strfind(data, [0 0 0 0]);
    producing:
    Idx =
    10
    .