MATLAB indexing by indexes in another matrix

Danny Garside

I'm trying to generate a new matrix, based on index values stored in another matrix.

This is trivial to do with a for loop, but this is currently the slowest line in some code I'm trying to optimise, and so I'm looking for a way to do it without the loop, and pulling my hair out. I'm sure this has been answered before, and that I just don't know the right search terms.

n1 = 10;
n2 = 100;

a = randi(n2,[1,n1]);
b = randi(n2,[4,n1]);
c = rand(100,100);

for i = 1:n1
    d(:,i) = c(a(i),b(:,i));
end
BillBokeey

I'm assuming the value of n1 in your code is way bigger than in the example you provide, which would explain why it is "slow".

In order to do this without a loop, you can use Linear indexing:

n1 = 1e6;
n2 = 100;

a = randi(n2,[1,n1]);
b = randi(n2,[4,n1]);
c = rand(n2,n2);

% With a loop
d = zeros(4,n1);
tic

for i = 1:n1
    d(:,i) = c(a(i),b(:,i));
end

toc

% A faster way for big values of `n1`
d2 = zeros(4,n1);
tic

a_rep = repmat(a,4,1); % Repeat row indexes to match the elements in b

idx_Lin = sub2ind([n2,n2],a_rep(:),b(:)); % Get linear indexes

d2(:) = c(idx_Lin); % Fill

toc

isequal(d,d2)

Elapsed time is 1.309654 seconds.

Elapsed time is 0.062549 seconds.

ans =

logical

1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Matrix indexing in matlab

Matlab Indexing Sparse Matrix

matlab matrix indexing of multiple columns

Indexing all diagonals of a matrix in MATLAB

indexing rows in matrix using matlab

Removing some indexes of a matrix in MATLAB

Matlab matrix and vector sorting for indexes

Numpy array indexing with another matrix

Array indexing: the fastest way to replace the indexes with another set of indexes in python

Indexing a vector with a matrix of indicies with numpy, similar to MATLAB

Sum of elements of a matrix (list), but only at indexes given in another matrix (list)

Matlab matrix indexing from 2 Arrays (X,Y)

Matlab: enter same vector repeatedly to matrix using logical indexing

Indexing a matrix in matlab according to conditions set on other matrices

How to get indexes of logical matrix without using find in matlab?

How to access matrix data in opencv by another mat with locations (indexing)

Compute the difference matrix between a matrix and another matrix in MATLAB

Insert values from matrix dataframe into another dataframe using indexes

Matrix Indexing with larger matrix

remove elements with corresponding zeros in another matrix matlab

There is a function in Matlab to create a matrix, where each element is the same function of matrix indexes?

How do I plot matrix in matlab, considering matrix indexes as co-ordinates on a x-y axis

Get dynamic rows of matrix based on the ones of another matrix (Matlab)

How to rearrange the elements of a matrix in Matlab according to the order of elements in another matrix?

Finding whether the rows of a matrix in Matlab "fall" within the rows of another matrix

How can I store a matrix in a row of another matrix? MATLAB

copying matrix values based on another matrix value in MATLAB

Can I pass the values of a matrix to another matrix by simultaneously indexing both matrices in c?

Vectorized creation of a 3D matrix by indexing one 2D matrix with another