Summing rows by index using accumarray

為{幸葍}努か 提交于 2019-12-01 11:29:11

Try this:

M_sum = (L + eye(size(L,1)))*M;
M_sum = triu(M_sum, 1);
M_sum = M_sum + M_sum.';

This works because you already have matrix L, so matrix multiplication can be used to select and sum the rows of M.


Using accumarray here would have two drawbacks:

  • You'd need to apply find to convert L into indices to be used as first input to accumarray. So one more step.
  • accumarray can only sum numbers, not row vectors (its second input can only be a column vector, not a matrix). So you'd need to call accumarray once per column of M.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!