MATLAB: Save the first non-zero value in each column

MATLABmatrix

Hi all
i have a matrix like that
1 0 1
0 1 0
0 0 0
1 0 1
i want to save only the first non zero values in each column and obtain:
1 0 1
0 1 0
0 0 0
0 0 0
How can i do that?
Thank you for the help
Regards

Best Answer

(cumsum(matrix) == 1) .* matrix