MATLAB: How can i count number of zeroes in a martix(containing elements 0s and 1s only) which is below any 1s(means in a column top to bottom , if 0 comes then dont count if 1 comes then count whole 0s below that 1.

matrix array

A(:,:,1) =
5 7 8
0 1 9
4 3 6
A(:,:,2) =
1 0 4
3 5 6
9 8 7

Best Answer

>> M = randi(0:1,5,6)
M =
0 1 1 1 0 1
0 1 0 0 0 1
1 0 0 0 1 0
1 1 1 0 0 1
0 0 0 1 1 1
>> sum(cumsum(M,1) & M==0, 1)
ans =
1 2 3 3 1 1