# How to sort a vector according to another vector?

3 Ansichten (letzte 30 Tage)
Cantor Set am 12 Feb. 2020
Beantwortet: dpb am 12 Feb. 2020
I am trying to write a code that will do thr following:
Input:
R=[a b c d]'; ranks=[1 2 3 1];
a has rank 1; b has rank 2;
c has rank 3; and d has rank 1;
I want to re-arrange R st that those elements whose ranks 1 are listed first followed by those who ranks 2 and so on.
So the output be:
R=[a d b c]'
This is a special case in other cases the vectors R and ranks will be very huge so I am trying to find a code that is efficient
Thank you
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

dpb am 12 Feb. 2020
R=['a':'d'].'; % So can identify elements of R
% the engine
[~,ix]=sort(ranks);
% the result...
>> R(ix)
ans =
4×1 char array
'a'
'd'
'b'
'c'
>>
The elements of R will have to be single values to be stored in an array or R would need be a cell array to hold other objects.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by