How can I order consecutive numbers?

조회 수: 1 (최근 30일)
Riccardo Rossi
Riccardo Rossi 2020년 6월 8일
답변: Walter Roberson 2020년 6월 8일
Hello everyone,
I need to reorder the values in A as follow (B):
(A) (B)
1 1
2 2
3 2
4 3
4 4
6 4
6 6
5 6
6 6
5 5
2 5
How can i do it? Thank you very much!
  댓글 수: 2
David Hill
David Hill 2020년 6월 8일
I don't recognize the pattern. What is the algorithm for the conversion?
Walter Roberson
Walter Roberson 2020년 6월 8일
I think it is a "stable" sort. The elements appear in the final vector in the same order they are introduced in the original. You can get the elements in that order with unique() with 'stable' option, but I am still working on a good way to get the desired final result.

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

채택된 답변

Walter Roberson
Walter Roberson 2020년 6월 8일
[~,~,R] = unique(A, 'stable');
[~,idx] = sort(R);
B = A(idx)
... or at least it worked for that example.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by