MATLAB: How to assign values to a sparse matrix without iterations

matrix operation

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

Best Answer

  • i = repmat( 1:size( Nonloc ), [1 2] );
    j = Nonloc(:)
    s = sparse( i, j, 1 );