MATLAB: Compare Row & only keep the highest value

keep highest valuesrow comparison

I have a matrix like this:
A:
100 0.35
200 0.37
300 0.42
400 0.40
500 0.39
600 0.46
I want the output like this:
B:
100 0.35
200 0.37
300 0.42
400 0.42
500 0.42
600 0.46
I.e. what I need is in the second column compare each row against previouos row. In case current row is higher than the previous row then keep it but if the previous row is higher then copy the prvious row in the current row.
I am aware that I can do it via a for loop. But is there any way to do it a more clean way through matrix operation?
Many thanks in advance for your help.

Best Answer

A = [100, 0.35; ...
200, 0.37; ...
300, 0.42; ...
400, 0.40; ...
500, 0.39; ...
600, 0.46];
B = [A(:, 1), cummax(A(:, 2))]
Or in your case:
B = cummax(A, 1) % Along 1st dimension