How to effectively replace the identifier vector with the vector of corresponding numerical values

Assuming a vector (or matrix) ids

>  1 2 3 3 2

Assume that each of these identifiers corresponds to a numerical value stored in another vector

14 33 25

I would like to replace identifiers with their corresponding value in order to build the following vector

14 33 25 25 33

There should be an easy way to achieve this without resorting to cycles, but my brain is currently not coping with me, and I could not find anything in the documentation. Any ideas?

+3
source share
2 answers

provided:

x = [14 33 25]

ind = [1 2 3 3 2]

then

x(ind) = 14 33 25 25 33
+8
source

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

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

Source: https://habr.com/ru/post/1744833/


All Articles