필터 지우기
필터 지우기

How to assign values to a sparse matrix without iterations?

조회 수: 5 (최근 30일)
Benson Gou
Benson Gou 2018년 10월 9일
댓글: dpb 2018년 10월 11일
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

채택된 답변

Adam
Adam 2018년 10월 9일
i = repmat( 1:size( Nonloc ), [1 2] );
j = Nonloc(:)
s = sparse( i, j, 1 );
  댓글 수: 1
Benson Gou
Benson Gou 2018년 10월 10일
Dear Adam,
Thanks a lot for your excellent code. It works very well.
Best regards, Bei

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2018년 10월 9일
S=sparse(Nonloc(:,1),Nonloc(:,2),1);
  댓글 수: 6
Benson Gou
Benson Gou 2018년 10월 11일
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 2018년 10월 11일
Ah! The light finally dawneth...the row isn't given but implied.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by