フィルターのクリア

Marix manipulation

2 ビュー (過去 30 日間)
Charles
Charles 2011 年 8 月 5 日
How do I perform this operation? I have a data set with recurring values at a given co-ordinate. I need to sum values of the repeating coordinates. I initially used a for loop to solve the problem but it is too slow. Here is a simple example of the problem:
val = rand(1);
a = [1 1 1 val;
1 2 1 val;
1 1 1 val];
Since the 1st and 3rd rows have the same co-ordinates, I need to add their values, delete the entries and replace with the new summed value. I know it should be possible using sub2ind and a cumsum operation, but I haven't figure exactly how to combine them yet. If I haven't written an answer at the time you are reading this, it means I still haven't got the combination right. Thanks.
  1 件のコメント
Pierre
Pierre 2011 年 8 月 5 日
>> _Since the 1st and 3rd rows have the same co-ordinates, I need to add their values, delete the entries and replace with the new summed value._
I wonder, if anybody can understand what you're trying to do there... I can't. :S
Please post your actual solution (with the for loop), this would allow us to see what the output's supposed to look like, would you?

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

採用された回答

Daniel Shub
Daniel Shub 2011 年 8 月 5 日
My solution has a for loop.
[b, i, j] = unique(a(:, 1:(end-1)), 'rows');
c = b;
for ii = 1:length(i)
c(ii, 4) = sum(a(j==ii, 4));
end
My guess is that it can be eliminated (and potentially made faster) with a bsxfun or arrayfun. In order to optimize it, we would need to know the sizes and how often things repeat.
  3 件のコメント
Charles
Charles 2011 年 8 月 5 日
ACCUMARRAY might be the better option as it doesn't involve a loop.
Daniel Shub
Daniel Shub 2011 年 8 月 5 日
While I am happy to have my answer accepted, my guess is that if you un-accept my answer (not even sure you can do that), once the people in the US wake up, you will get a bsxfun/arrayfun/accumarray solution.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by