Rearrange an array based on another array

조회 수: 5 (최근 30일)
luca
luca 2019년 10월 4일
댓글: luca 2019년 10월 4일
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?

채택된 답변

Stephen23
Stephen23 2019년 10월 4일
편집: Stephen23 2019년 10월 4일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 C Shared Library Integration에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by