MATLAB: Remove elements from a matrix

matrix manipulation

Hello,
I have a matrix 1080×840 filled with 0 and 1, like:
[1 1 1 1 1 1 0 0 0 0 0 ;
1 1 1 1 1 1 1 1 0 0 0 1;
1 1 1 1 1 0 0 0 1 0 0 1;
1 1 1 1 1 1 0 0 0 1 0 1]
where a "zero' appears I would like to change the ones located in the right of the zero to zero. So, the new matrix would be:
[1 1 1 1 1 1 0 0 0 0 0;
1 1 1 1 1 1 1 1 0 0 0 0;
1 1 1 1 1 0 0 0 0 0 0 0;
1 1 1 1 1 1 0 0 0 0 0 0]
How can I do that?

Best Answer

Very simply using cumprod:
>> M = [1,1,1,1,1,1,0,0,0,0,0,0;1,1,1,1,1,1,1,1,0,0,0,1;1,1,1,1,1,0,0,0,1,0,0,1;1,1,1,1,1,1,0,0,0,1,0,1]
M =
1 1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 1 1 0 0 0 1
1 1 1 1 1 0 0 0 1 0 0 1
1 1 1 1 1 1 0 0 0 1 0 1
>> cumprod(M,2)
ans =
1 1 1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 1 1 1 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0
1 1 1 1 1 1 0 0 0 0 0 0
Related Question