MATLAB: How to find the index of any value in array (during for loop)

find index in for loop

Hello everyone,
I have one array e.g.
a= [1 1 1 1 0 0 1 1 1 0 0 0 0 1 0 0 0 0 1 0
0 1 1 1 1 1 1 0 0 1 1 0 0 0 0 1 0 1 0 0
1 1 1 0 0 0 0 1 0 1 0 0 1 0 1 0 1 0 0 0
0 0 0 1 0 0 1 0 1 0 1 1 1 1 1 1 0 0 0 1];
for this example I want to make groups of 4 so for this case we will get 5 groups for one row.
so, for every group I want to check the index of '1s' from this original array a.
for example,
for 2nd group i.e. [0 0 1 1] and I want to check that index where there is one in this case I want the index should be 7 and 8
I used find(a(i,j)==1) in two for loops but this line always giving '1' index
Can you please help me to solve this problem??

Best Answer

This seems to work:
a = [1 1 1 1 0 0 1 1 1 0 0 0 0 1 0 0 0 0 1 0
0 1 1 1 1 1 1 0 0 1 1 0 0 0 0 1 0 1 0 0
1 1 1 0 0 0 0 1 0 1 0 0 1 0 1 0 1 0 0 0
0 0 0 1 0 0 1 0 1 0 1 1 1 1 1 1 0 0 0 1]
[rows, columns] = size(a)
for row = 1 : rows
thisRow = a(row,:)
counter = 0;
for col = 1 : 4 : columns
counter = counter + 1;
thisSegment = thisRow(col:col+3)
f = find(thisSegment);
if ~isempty(f)
output{row, counter} = col+f - 1;
end
end
end
celldisp(output)