MATLAB: How to find elements between zeros in matrix

find elements

Hello, I have this matrix 3×3:
A = [3,4,6,0,2,7,10,5,0,1,0,8,9,0;
0,5,2,0,1,8,9,0,4,6,10,7,0,3;
1,0,7,2,0,4,10,8,0,3,5,0,6,9]
I want to find(specify) elements between zeros for each row, so I can apply some conditions on these elements between zeros. How can I do it with loops and also for large size matrices.

Best Answer

Try this:
A = [3,4,6,0,2,7,10,5,0,1,0,8,9,0; 0,5,2,0,1,8,9,0,4,6,10,7,0,3; 1,0,7,2,0,4,10,8,0,3,5,0,6,9]
[rows, columns] = size(A);
for row = 1 : rows
% Extract just this one row from A.
thisRow = A(row, :);
% Get each group of non-zeros in this row.
props = regionprops(thisRow~=0, thisRow, 'PixelValues');
numGroups = length(props); % Get how many groups of non-zeros there are.
% Examine each group for meeting "constraints"
for g = 1 : numGroups
thisGroup = props(g).PixelValues
% Now do something with this group.
% Check constraints or whatever you want to do.
end
end