Eric
Eric

Reputation: 2128

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 in 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

Upvotes: 5

Views: 6114

Answers (1)

Pablo
Pablo

Reputation: 8644

Use B(A). It treats the elements of A as indices into B and returns the an array with the same size as A.

Upvotes: 7

Related Questions