Rearrange an array based on another array

5 vues (au cours des 30 derniers jours)
luca
luca le 4 Oct 2019
Commenté : luca le 4 Oct 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?

Réponse acceptée

Stephen23
Stephen23 le 4 Oct 2019
Modifié(e) : Stephen23 le 4 Oct 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

Plus de réponses (0)

Catégories

En savoir plus sur C Shared Library Integration dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by