Borrar filtros
Borrar filtros

sub2ind, picking specific values from a 3d matrix

3 visualizaciones (últimos 30 días)
Kim
Kim el 9 de Jun. de 2014
Respondida: Dan Nussinson el 10 de Mayo de 2019
I have a 500x8x4 matrix, lets call this X, and I have a 500x1 vector, lets call it Y, the latter contains numbers between 1 and 8. These numbers indicate the number in each row that I want from the matrix X. So by picking a number in each row of matrix X, I need to get a matrix that is 500x4, lets call it Z. I dont know how to get this vector Z, in a quick way (not using a loop). I know I should do it with sub2ind but I dont know how. Thank you for your help.

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 9 de Jun. de 2014
Editada: Sean de Wolski el 9 de Jun. de 2014
sub2ind is slow, if you want performance, use a for-loop.
% Data
X = randn(500,8,4); % z
Y = randi(8,500,1); % idx
% Engine
szX = size(X);
Z = zeros(szX([1 3]));
for ii = 1:szX(1)
Z(ii,:) = X(ii,Y(ii),:);
end
On my machine, the engine on here is taking about 1/1000 of a second.

Más respuestas (1)

Dan Nussinson
Dan Nussinson el 10 de Mayo de 2019
Sean is right when the matrices are small as in your example. For large for loops sub2ind is x10 faster on my system:
% Data
X = randn(50000,8,4); % z
Y = randi(8,50000,1); % idx
% Engine
szX = size(X);
tic
Z = zeros(szX([1 3]));
for ii = 1:szX(1)
Z(ii,:) = X(ii,Y(ii),:);
end
toc
%sub2ind
tic
ii = 1:szX(1);
linInds = sub2ind(szX([1 2]),ii',Y);
X2 = reshape(X,szX(1)*szX(2),[]);
Z2 = X2(linInds,:);
toc

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by