Basic Vectorization Question (speed required)
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Matlab2010
el 22 de Nov. de 2013
Comentada: Matlab2010
el 22 de Nov. de 2013
I have a 2D matrix of data. I have an index to reduce that 2D matrix to a 1D matrix.
How can I do it without using a for loop? Ie by vectorization as it needs to be super quick.
thanks
Z = rand(5,100); %matrix of data
IDY = randi([1 5], [1,100]); %index into that matrix
X = NaN(size(Z,2),1); %pre allocate
X = Z(IDY,:); %this fails as it does not generate a 1D matrix (vector) as required
this is how I can do it using a loop
X2 = NaN(size(Z,2),1);
for i = 1: size(X,1)
X2(i) = Z(IDY(i),i);
end
0 comentarios
Respuesta aceptada
Azzi Abdelmalek
el 22 de Nov. de 2013
Z = rand(5,100); %matrix of data
IDY = randi([1 5], [1,100]); %index into that matrix
X = NaN(size(Z,2),1); %pre allocate
X = Z(IDY,:)
X3=Z(sub2ind(size(Z),IDY,1:size(Z,2)))
Más respuestas (1)
Image Analyst
el 22 de Nov. de 2013
To get all of Z in row-major order, do:
X = Z(:);
To get random ordering, like your code attempted to do, try this:
Z = rand(5,100) %matrix of data
randomIndexes = randperm(numel(Z)) %index into that matrix
X = Z(randomIndexes)
1 comentario
Ver también
Categorías
Más información sobre Multidimensional Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!