フィルターのクリア

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])
Jonathan Ron
Jonathan Ron 2012 年 8 月 21 日
tnx guys

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by