MATLAB: Extend / replicate a value by column when found in array

matrix arraymatrix manipulation

Say I have a matrix 5×5 with binary entries 0 and 1
A=[1 1 1 1 1;1 0 1 1 1;1 0 1 0 0;0 1 1 0 1;1 1 1 0 0]
I'd like to have as a result, a matrix where when the array is parsed by column, if 0 is found, then all rows indexed after are 0:
B=[1 1 1 1 1;1 0 1 1 1;1 0 1 0 0;0 0 1 0 0;0 0 1 0 0]
Any clue for a fast computing solution ? FYI every column is independent.

Best Answer

This is trivially achieved with cumprod since as soon as a 0 is encountered in a column the cumulative product is 0 from then on:
A = [1 1 1 1 1;1 0 1 1 1;1 0 1 0 0;0 1 1 0 1;1 1 1 0 0]
B = cumprod(A)