Rearrange an array based on another array

3 views (last 30 days)
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?

Accepted Answer

Stephen23
Stephen23 on 4 Oct 2019
Edited: Stephen23 on 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

More Answers (0)

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!