Indexing a matrix with another matrix?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Petter Stefansson
el 23 de Sept. de 2016
Comentada: Petter Stefansson
el 23 de Sept. de 2016
Hi.
I have a 10000x4 matrix, Idx, where each row contains 4 indices to a logical matrix, A. I would like to set all indices in Idx to true (1) in A. This seems like something that should be possible to do very easy in one single line but I can’t quite figure it out so I’m currently doing it by looping:
A = false(10000,256);
for i = 1 : size(A,1)
A(i,Idx(i,:)) = true;
end
which is very slow. Could someone tell me how to do this indexing without a loop?
Thanks
0 comentarios
Respuesta aceptada
Andrei Bobrov
el 23 de Sept. de 2016
Editada: Andrei Bobrov
el 23 de Sept. de 2016
[m,n] = size(Idx);
[ii,~] = ndgrid(1:m,1:n); % or ii = repmat((1:m)',1,n);
% ii = (1:m)'*ones(1,n);
A(sub2ind(size(A),ii,Idx)) = true;
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!