Effacer les filtres
Effacer les filtres

assiging values to matrix accrding to indexes using sub2ind

1 vue (au cours des 30 derniers jours)
Jonathan Ron
Jonathan Ron le 21 Août 2012
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

Réponse acceptée

Honglei Chen
Honglei Chen le 21 Août 2012
Modifié(e) : Honglei Chen le 21 Août 2012
Try accumarray
MAT = accumarray([Z X],DATA)
  4 commentaires
Andrei Bobrov
Andrei Bobrov le 21 Août 2012
accumarray([Z(:),X(:)],DATA(:),[m n])
Jonathan Ron
Jonathan Ron le 21 Août 2012
tnx guys

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by