Borrar filtros
Borrar filtros

How to assign values to a sparse matrix without iterations?

5 visualizaciones (últimos 30 días)
Benson Gou
Benson Gou el 9 de Oct. de 2018
Comentada: dpb el 11 de Oct. de 2018
Hi, All,
I want to assign non-zero values to a matrix H. I know the locations of all the non-zeros. For example, I know the non-zero locations:
It indicates H(1,1)=1;H(1,2)=1;H(2,1)=1;H(2,5)=1;H(3,2)=1;H(3,3)=1;...
Would you please tell me how I can quickly assign this values without iterations?
Thanks a lot. Bei

Respuesta aceptada

Adam
Adam el 9 de Oct. de 2018
i = repmat( 1:size( Nonloc ), [1 2] );
j = Nonloc(:)
s = sparse( i, j, 1 );
  1 comentario
Benson Gou
Benson Gou el 10 de Oct. de 2018
Dear Adam,
Thanks a lot for your excellent code. It works very well.
Best regards, Bei

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 9 de Oct. de 2018
S=sparse(Nonloc(:,1),Nonloc(:,2),1);
  6 comentarios
Benson Gou
Benson Gou el 11 de Oct. de 2018
Hi, dab and Bruno,
Thanks for your comments. I think dab's code gives one "1" in each row while Adam gives two "1" in each row which is expected.
Thanks a lot again. Bei
dpb
dpb el 11 de Oct. de 2018
Ah! The light finally dawneth...the row isn't given but implied.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices 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