MATLAB: Check and eliminate rows based on conditions 2

matrix

Hi. I have a matrix as below. I need help to code this problem.
A =
[1.0000 -0.1367 0.0366 0.2431 0.1541 0.2263 0.3913 -0.1903
-0.1367 1.0000 -0.3278 -0.1718 -0.3803 -0.1378 -0.0839 0.2210
0.0366 -0.3278 1.0000 0.2087 0.4730 0.2077 0.0717 -0.1989
0.2431 -0.1718 0.2087 1.0000 0.2513 0.3382 0.5135 -0.3838
0.1541 -0.3803 0.4730 0.2513 1.0000 0.1547 0.0867 -0.1398
0.2263 -0.1378 0.2077 0.3382 0.1547 1.0000 0.5096 -0.4888]
1. Therefore, I have to check every row in third column.
2. For every row in third column, I have to ensure that all previous rows have more value than current row value. If any previous row value in third column is less than current value, that previous row will be eliminated. Else, previous row will maintain.
3. For matrix A, as result, all rows except row 3, 5, and 6 will eliminate. End result after check and eliminate will produce matrix B as below.
A =
0.0366 -0.3278 1.0000 0.2087 0.4730 0.2077 0.0717 -0.1989
0.1541 -0.3803 0.4730 0.2513 1.0000 0.1547 0.0867 -0.1398
0.2263 -0.1378 0.2077 0.3382 0.1547 1.0000 0.5096 -0.4888

Best Answer

[~,ii] = sort(A(:,3),'descend');
for MATLAB >= R2016b
out = A(ii(all(tril(ii < ii')==0,2)),:);
for MATLAB <= R2016a
out = A(ii(all(tril(bsxfun(@lt,ii(:),ii(:)'))==0,2)),:);