MATLAB: Mapping Values to Index of Other Array
Can any MATLAB experts help out with this:
I have the following two arrays:
A = [1 1 3 4 4 4 4 4];
B = [6 7 8 9];
I would like to make a third array that uses the values of "A" as sort of pointers to the array in B. So, the final result would be:
C = [6 6 8 9 9 9 9 9];
Every element of "A" is mapped to an index in "B".
Thanks i开发者_运维技巧n advance.
Edit: Sorry, forgot to mention: I'm looking for a non-loop solution. This would work (I think), but it uses looping:
C = [];
for i = 1:length(A)
C = [C B(A(i))];
end
Use B(A)
. It treats the elements of A
as indices into B
and returns the an array with the same size as A
.
精彩评论