필터 지우기
필터 지우기

assiging values to matrix accrding to indexes using sub2ind

조회 수: 1 (최근 30일)
Jonathan Ron
Jonathan Ron 2012년 8월 21일
I have 3 data sets, two with coordinates and one with data with the length of n
with a loop I would assign the data in this way
MAT=zeros(m,n);
for i=1:n
MAT(Z(i),X(i))=MAT(Z(i),X(i))+DATA(i);
end
I want to do it without a loop since
what I am trying to do is something like
MAT=zeros(m,n);
mn=size(MAT);
MAT(sub2ind(mn,Z,X))=MAT(sub2ind(mn,Z,X))+DATA;
any one has an idea how to make it properly and efficiently?
cheers

채택된 답변

Honglei Chen
Honglei Chen 2012년 8월 21일
편집: Honglei Chen 2012년 8월 21일
Try accumarray
MAT = accumarray([Z X],DATA)
  댓글 수: 4
Andrei Bobrov
Andrei Bobrov 2012년 8월 21일
accumarray([Z(:),X(:)],DATA(:),[m n])

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by