Removing duplicate elements & choose maximum row value of matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello,
I have an matrix, which have repeated values at row 8, I want to choose the row with maximum value in elements in column 4 and discard the rows with repeated value in column 8.
A= [1973 2 12 0.89518 1973 2 14 1468
1973 4 3 1.1867 1973 3 24 1903
1973 10 21 1.1006 1973 10 24 1307
1973 11 6 0.91814 1973 11 3 1913
1973 11 13 0.9313 1973 11 3 1913
1973 11 19 1.2501 1973 11 9 1618
1973 11 25 1.1185 1973 12 1 2953
1973 12 7 1.3553 1973 12 1 2953];
I would like to delete 4th & 7th row in this case. The desired output is:
1973 2 12 0.89518 1973 2 14 1468
1973 4 3 1.1867 1973 3 24 1903
1973 10 21 1.1006 1973 10 24 1307
1973 11 13 0.9313 1973 11 3 1913
1973 11 19 1.2501 1973 11 9 1618
1973 12 7 1.3553 1973 12 1 2953
Any help?
댓글 수: 0
채택된 답변
Andrei Bobrov
2017년 11월 17일
편집: Andrei Bobrov
2017년 11월 17일
[~,~,c] = unique(A(:,end),'stable');
out = A(accumarray(c,(1:size(A,1))',[],@(x)x(max(A(x,4)) == A(x,4))),:);
추가 답변 (1개)
KSSV
2017년 11월 17일
편집: KSSV
2017년 11월 17일
Read about unique and max.
A = [1973 2 12 0.89518 1973 2 14 1468
1973 4 3 1.1867 1973 3 24 1903
1973 10 21 1.1006 1973 10 24 1307
1973 11 6 0.91814 1973 11 3 1913
1973 11 13 0.9313 1973 11 3 1913
1973 11 19 1.2501 1973 11 9 1618
1973 11 25 1.1185 1973 12 1 2953
1973 12 7 1.3553 1973 12 1 2953];
[maxval,idx] = max(A(:,4)) ;
idx = find(A(:,8)==A(idx,8)) ;
A(idx(2:end),:) = []
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!