How can I delete the duplicate rows in cell based on a particular column?

The input is:
A={[1 2 3] [2 3] [1 2 3];[2 5 8] [3 4] [1 2 3];[2 5 4] [2 3] [1 2 3];[1 2 5] [3 4] [1 2 3]};
based on the duplication in the 2nd column,the output will be:
B={[2 5 4] [2 3] [1 2 3];[1 2 5] [3 4] [1 2 3]};
How can I do that?

댓글 수: 2

Matt J
Matt J 2020년 8월 26일
편집: Matt J 2020년 8월 26일
How do you know which of the duplicates to keep? Why couldn't you keep the first two rows instead of the last two?
That's really not matter for my case. I just want to delete the duplicate rows based on the 2nd column.

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

 채택된 답변

Matt J
Matt J 2020년 8월 26일
편집: Matt J 2020년 8월 26일
This assumes the second column always contains vectors of the same length, and also that you want to keep only the last occurence.
[~,idx]=unique(cell2mat(A(:,2)),'rows','last');
B=A(idx,:);

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Types에 대해 자세히 알아보기

태그

질문:

SM
2020년 8월 26일

댓글:

SM
2020년 8월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by