Sort a matrix based on unique values
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi,
Lets assume I have matrix M:

And I want to fill in matrix O:

I want to sort the final matrix based on the first row in matrix M and O. If I just fill in matrix O without sorting, I get this result:

This is wrong, because colum 3 should be NaN while column 4 should have the values from matrix O, because there is a "match" between the first rows (1004 and 1004).
I hope it was clear. Thanks
댓글 수: 2
Stephen23
2020년 4월 4일
Use ismember.
If you had given data as text then I would have shown you how, but I can't do anything with data in screenshots.
채택된 답변
the cyclist
2020년 4월 4일
I think you can use the ismember function to do what you want. For example,
[tf,loc] = ismember([1001 1002 1004],[1001 1002 1003 1004 1005]);
will result in
loc = [1 2 4]
skipping the 3rd column as you want.
So, you probably want something like
[tf,loc] = ismember(O(1,:),M(1,:));
to find your indices.
댓글 수: 3
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!