MATLAB: Extracting data from an array

array

Hello guys,
I've a (14×3) array from which I need to extract data and save in two separate arrays.
I only need the data when in column 2, the value changes from 0 to 1 and back to 0. The data has to be extracted from 0 to 0. There are two sets of such occurrences and I need to save these in two separate arrays. Could you please help me solve this?
Thanks in advance Kind Regards Hari

Best Answer

As Azzi mentions, it is not perfectly clear what you want as your output, but here is my best guess at the code you need:
A = [1 0 0.1
2 0 0.3
3 1 0.7
4 2 0.4
5 2 0.7
6 1 0.8
7 0 0.3
8 0 0.7
9 0 0.4
10 1 0.7
11 2 0.5
12 2 0.1
13 1 0.8
14 0 0.2];
zeroIndices = find(A(:,2)==0);
numberZeros = numel(zeroIndices);
cellNumber = 0;
for nz = 1:numberZeros-1;
if zeroIndices(nz+1) > zeroIndices(nz)+1
cellNumber = cellNumber + 1;
C{cellNumber} = A(zeroIndices(nz):zeroIndices(nz+1),:);
end
end
Since you said you want "0 to 0", I included those rows. If you really only wanted the non-zero rows, use this instead:
C{cellNumber} = A(zeroIndices(nz)+1:zeroIndices(nz+1)-1,:);