Matrix problem for same values of column
이전 댓글 표시
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.
댓글 수: 1
Is the row
28.43 17 36
correct in your example output array? Following your explanation, these rows are one group:
28.43 17 36
...
27.36 36 41
...
28.61 41 49
of which the last row has the highest values in the first column (and the last row is in your output array). But why do you keep the first row as well?
채택된 답변
추가 답변 (1개)
Andrei Bobrov
2019년 9월 2일
[m,n] = size(A);
B = [(1:m)',A(:,2:3)];
k = B(1,2:3);
ii = 1;
C{1} = [];
while ~isempty(B)
i0 = ismember(B(:,2:3),k);
lo = any(i0,2);
if any(lo)
C{ii} = [C{ii};[repmat(ii,nnz(lo),1),B(lo,1)]];
k = B(xor(i0(:,1),i0(:,2)),2:3);
B = B(~lo,:);
else
ii = ii + 1;
k = B(1,2:3);
C{ii} = [];
end
end
iii = cat(1,C{:});
T = array2table(A);
T = T(iii(:,2),:);
T.g = iii(:,1);
T = sortrows(T,{'g','A1'},{'ascend','descend'});
T = rowfun(@(x,y,z)[x(1),y(1),z(1)],T,'GroupingVariables','g');
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!