필터 지우기
필터 지우기

Removing Rpeated Values from a Matrix

조회 수: 2 (최근 30일)
Sarvesh Kumar
Sarvesh Kumar 2018년 6월 8일
댓글: Sarvesh Kumar 2018년 6월 8일
Hi
I have a matrix below:
NaN NaN
518460 3.3220
518460 -12.9840
518520 3.7890
518520 -10.2800
518580 7.6730
518580 16.8510
518640 5.9590
.
.
.
the values in the 1st column are repeated (2-4 times). i want to write a code which checks the values in the 1st column, if it is repeated, then it will read the respective 2nd column values and only keep the row with the largest 2nd column value while deleting the other rows. so the matrix above when ran through the code should be:
518460 3.3220
518520 3.7890
518580 7.6730
518580 16.8510 .
.
.
the data exceeds over 1000 entries but i have only given a small piece of it above.
how can i write the code so i get my second matrix from the first.

채택된 답변

Stephen23
Stephen23 2018년 6월 8일
편집: Stephen23 2018년 6월 8일
>> M = [518460,3.3220;518460,-12.9840;518520,3.7890;518520,-10.2800;518580,7.6730;518580,16.8510;518640,5.9590]
M =
518460 3.32200
518460 -12.98400
518520 3.78900
518520 -10.28000
518580 7.67300
518580 16.85100
518640 5.95900
>> [uni,~,idx] = unique(M(:,1));
>> val = accumarray(idx,M(:,2),[],@max);
>> [uni,val]
ans =
518460 3.32200
518520 3.78900
518580 16.85100
518640 5.95900
  댓글 수: 1
Sarvesh Kumar
Sarvesh Kumar 2018년 6월 8일
thank you very much. it worked like a charm! I thought it would be a long code, did not expect a 3 line command! thanks alot once again.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by