MATLAB: Matrix problem for same values of column

columnmatrixrepeated valuesrows

A=[29.78 5 8
24.97 8 11
22.98 4 12
21.05 12 13
24.78 1 16
25.53 26 29
21.43 2 32
29.94 11 33
29.57 15 35
28.43 17 36
11.49 23 37
13.69 37 38
26.97 28 39
16.25 25 40
27.36 36 41
4.24 18 42
19.39 39 44
29.93 16 45
25.83 30 46
26.09 40 47
27.58 24 48
28.61 41 49
29.41 48 50]
and i want
output =[22.98 4 12
25.53 26 29
21.43 2 32
29.94 11 33
29.57 15 35
28.43 17 36
13.69 37 38
26.97 28 39
4.24 18 42
29.93 16 45
25.83 30 46
26.09 40 47
28.61 41 49
29.41 48 50 ]
No value in column 2, 3 get repeated and in case of repeated value in any of the column(2,3) the higest value of column 1 is as the output.
For example, in row 1, 2 and 8. column (2,3) have values as
[ 5 8
8 11
11 33]
Among these 3 rows row 8, ie. [29.94 11 33] have the highest value so only this row will be the output. all other row like [29.78 5 8] and [24.97 8 11]will be elimanted.
simillarly,
for row 3 = [22.98 4 12]
And 4 = [21.05 12 13]
row 3= [22.98 4 12]
will be output and row 4 will get eliminated.

Best Answer

N = size(A,1);
X = ones(N,1); % group numbers
Z = true(N,1); % logical index
V = 1; % group number
for k = 2:N % for each row...
Y = A(k,2)==A(1:k-1,3); % check if any matching rows.
if any(Y)
X(k) = X(Y); % copy group number (assumed scalar).
W = X(k)==X(1:k-1); % logical index of that group.
if all(A(k,1)>A(W,1))
Z(W) = false; % current val > prev vals.
else
Z(k) = false; % prev val > current val.
end
else % no matching rows:
V = V+1; % increment group number.
X(k) = V;
end
end
B = A(Z,:) % output matrix
Giving:
B =
22.98 4 12
25.53 26 29
21.43 2 32
29.94 11 33
29.57 15 35
13.69 37 38
26.97 28 39
4.24 18 42
29.93 16 45
25.83 30 46
26.09 40 47
28.61 41 49
29.41 48 50