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 Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by