MATLAB: How to check whether a 2d matrix is gradually increasing in values in row direction.

image processingmatrix manipulation

Lets say u have a matrix A=[2 4 7;3 4 6;] So we can see the A(4)==3 in row 2 has increased from A(1)==2 progression,
And the 6th element,A(6)==6 has reduced from being A(3)==7 to 6.
So the A(6) needs to be replaced by Nan
This is basically the thing. Needs to be done in a large matrix. Any ideas on doing it faster than for loops.

Best Answer

Using cummax is simple:
>> A = [2,4,7;3,4,6]
A =
2 4 7
3 4 6
>> A(A<cummax(A,1)) = NaN
A =
2 4 7
3 4 NaN
EDIT: to also ignore adjacent repeated values:
>> A = [2,4,7;3,4,6]
A =
2 4 7
3 4 6
>> idx = A<cummax(A,1) | 0==diff([NaN*A(1,:);A],1,1);
>> A(idx) = NaN
A =
2 4 7
3 NaN NaN