Matrix Indexing

10 visualizaciones (últimos 30 días)
Joerg
Joerg el 4 de Feb. de 2011
I have an index matrix.
idx = [1 5 ; 3 7 ]
The goal is to get a matrix like this:
1 0
0 0
0 1
0 0
1 0
0 0
0 1
Indexing the matrix like
a(idx) = 1;
doesn't work. Right now I use a loop, but I am not sure if this is the best way.

Respuesta aceptada

Matt Fig
Matt Fig el 4 de Feb. de 2011
A = zeros(max(idx(:)),size(idx,1));
A(sub2ind(size(A),idx,cumsum(ones(size(idx))))) = 1
  1 comentario
Joerg
Joerg el 7 de Feb. de 2011
Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing 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