Assuming a vector (开发者_Python百科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?
assuming:
x = [14 33 25]
ind = [1 2 3 3 2]
then
x(ind) = 14 33 25 25 33
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])
精彩评论