Rearrange an array based on another array

3 visualizaciones (últimos 30 días)
luca
luca el 4 de Oct. de 2019
Comentada: luca el 4 de Oct. de 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?

Respuesta aceptada

Stephen23
Stephen23 el 4 de Oct. de 2019
Editada: Stephen23 el 4 de Oct. de 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

Más respuestas (0)

Categorías

Más información sobre C Shared Library Integration en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by