MATLAB: Max/Min of nonzero rows/cols

max/min

I have a Matrix, A.
I want to find the max and min for every row and column in A, excluding the zero entries.
I have coded it using For loops, but it is too slow. I need it to be all vectorized. Any help is greatly appreciated.

Best Answer

B = A;
B(B == 0) = NaN;
colMax = max(B);
colMin = min(B);
rowMax = max(B, [], 2);
rowMin = min(B, [], 2);
edit: missed the bit about the zero entries