Using accumarray to do sum with duplicate indicies

조회 수: 5 (최근 30일)
Paul Chesler
Paul Chesler 2017년 9월 24일
댓글: Paul Chesler 2017년 9월 24일
Hi folks, Let f and A be arrays with size(f) = [N,1] and size(A) = [M,1] with N > M. Let J be a list of indices for f with size(J) = size(A). I wish to compute the for loop
for k = 1:M,
f(J(k)) = f(J(k)) + A(k);
end
However, due to time constraints I need to do this computation without the for loop. If all the indices J were unique I would simply say f(J) = f(J) + A. However, I will need to consider the case where there are many duplicate indices in J. I suspect this can be accomplished with accumarray, but its not clear to me how to do this. Does anybody have any suggestions?
Thanks, Paul
  댓글 수: 1
the cyclist
the cyclist 2017년 9월 24일
It would be helpful to include a small example that is general enough for your use case, showing what the input and expected output are.

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

채택된 답변

Matt J
Matt J 2017년 9월 24일
편집: Matt J 2017년 9월 24일
Yes, it is a direct application of accumarray,
f=f+accumarray(J,A,size(f));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by