fricadelle
fricadelle

Reputation: 477

matlab retrieve elements in a vector corresponding to matrix of indexes

I have a vector v.
I also have a matrix M of size TxN with each column corresponding to T indices of v.
For example: M(:,1) is the set of indices [1,2,12,5,4] (here T = 5).

I want to have a matrix O of size TxN with O(:,i) = v(M(:,i)) for all i.
Is there a way to do that without using for loops ?

Thanks a lot

Upvotes: 0

Views: 60

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112759

Very easy: just use

O = v(M);

Example with T=3, N=4:

>> v = (10:10:50).'
v =
    10
    20
    30
    40
    50
>> M = randi(5,T,N)
M =
     5     3     5     3
     2     3     1     4
     2     4     5     3
>> O = v(M)
O =
    50    30    50    30
    20    30    10    40
    20    40    50    30

Upvotes: 1

Related Questions