开发者

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

开发者 https://www.devze.com 2022-12-30 00:37 出处:网络
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

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])
0

精彩评论

暂无评论...
验证码 换一张
取 消