Kena
Kena

Reputation: 6921

How to efficiently replace a vector of ids by a vector of corresponding numerical values

Assuming a vector (or matrix) of ids

>  1 2 3 3 2

Let's suppose each of these ids corresponds to a numerical value, stored in another vector

14 33 25

I'd like to replace the ids by their corresponding value to build the following vector

14 33 25 25 33

There must be a simple way to achieve this without resorting to loops, but my brain fails me at the moment, and I couldn't find anything in the documentation. Any ideas?

Upvotes: 3

Views: 324

Answers (2)

Olivier Verdier
Olivier Verdier

Reputation: 49196

For what it's worth, it works also with python+numpy:

x = array([14,33,25])
ind = [0,1,2,2,1]
x[ind] # -> array([14, 33, 25, 25, 33])

Upvotes: 0

Nathan Fellman
Nathan Fellman

Reputation: 127538

assuming:

x = [14 33 25]

ind = [1 2 3 3 2]

then

x(ind) = 14 33 25 25 33

Upvotes: 8

Related Questions