Rearrange an array based on another array

6 visualizzazioni (ultimi 30 giorni)
luca
luca il 4 Ott 2019
Commentato: luca il 4 Ott 2019
Hi, given the following array
SPI = [1 2 3 4 5 6 7 8 9 10 11 12 13];
VI = [ 5 20 10 15 5 20 10 15 10 15 20 10 5 ];
each value of SPI has a corrispondent value in VI. 1-5 , 2-20 , 3-10 and so on
I have obtained now a new vector SP with all the values that were in SPI
SP = [9 3 11 7 4 2 13 12 8 10 5 6 1]
And I want to obtain a new vector V where I have the corrispondent values of VI for each element of SP.. so:
V = [10 10 20 10 15 20 5 10 15 15 5 20 5]
May someone can help me?

Risposta accettata

Stephen23
Stephen23 il 4 Ott 2019
Modificato: Stephen23 il 4 Ott 2019
General solution:
>> [~,idx] = ismember(SP,SPI);
>> V = VI(idx)
V =
10 10 20 10 15 20 5 10 15 15 5 20 5
Solution specifically for your example SPI which is defined as 1:numel(VI):
>> V = VI(SP)
V =
10 10 20 10 15 20 5 10 15 15 5 20 5

Più risposte (0)

Categorie

Scopri di più su C Shared Library Integration in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by