Borrar filtros
Borrar filtros

assiging values to matrix accrding to indexes using sub2ind

1 visualización (últimos 30 días)
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

Respuesta aceptada

Honglei Chen
Honglei Chen el 21 de Ag. de 2012
Editada: Honglei Chen el 21 de Ag. de 2012
Try accumarray
MAT = accumarray([Z X],DATA)
  4 comentarios
Andrei Bobrov
Andrei Bobrov el 21 de Ag. de 2012
accumarray([Z(:),X(:)],DATA(:),[m n])

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by