MATLAB: Once a row value is zero, set all values after that point in that row to zero

logical indexingzeros

I have a matrix made up of 41 rows by 20000 columns. I will use the following smaller matrix for an illustration x = [1 2 2 6 8; 1 0 1 3 7; 1 6 3 3 3; 1 7 0 0 4; 1 5 5 5 5];
When a value in a row goes to 0, I want to then force all other values in that row, after that point, to equal 0. So for example my x would then equal: 1 2 2 6 8; 1 0 0 0 0; 1 6 3 3 3; 1 7 0 0 0; 1 5 5 5 5
I can do it with two for loops and an if statement, but does anyone know of a quicker/easier way?

Best Answer

out=A.*(cumprod(A,2)~=0)