How can I order consecutive numbers?

2 Ansichten (letzte 30 Tage)
Riccardo Rossi
Riccardo Rossi am 8 Jun. 2020
Beantwortet: Walter Roberson am 8 Jun. 2020
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 Kommentare
David Hill
David Hill am 8 Jun. 2020
I don't recognize the pattern. What is the algorithm for the conversion?
Walter Roberson
Walter Roberson am 8 Jun. 2020
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Weitere Antworten (0)

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by