How to filter and group data in matlab

3 ビュー (過去 30 日間)
Léon
Léon 2012 年 11 月 28 日
Hello,
I try to group data in matlab by doing:
Consider a dataset like the following:
data = dataset;
data.groups_a = [1;1;2;1;2;2;2];
data.groups_b = [7;8;9;7;8;9;9];
data.values = [103;187;261;366;271;287;311];
%%Now let's sum up the values for each group in groups_a
[groups_a,~,ICa] = unique(data.groups_a);
[groups_b,~,ICb] = unique(data.groups_b);
groupped_by_a = accumarray(ICa,data.values,[],@nansum);
So this works excellent is quite fast even for large datasets. But how can I now retrieve the values grouped by groups_a AND groups_b? So my matrix should be:
x = zeros(length(groups_a),length(groups_b));
Example:
x(1,1) should be the sum of all values that are in group_a == 1 and group_b == 7. So the correct entry would be x(1,1) = 103 + 366;
Can somebody may point me to the solution? Thank you very much beforehand!
Regards!

採用された回答

Walter Roberson
Walter Roberson 2012 年 11 月 29 日
accumarray( [ICa, ICb], data.values, [], @nansum)
  1 件のコメント
Léon
Léon 2012 年 11 月 29 日
So close ;)
Thank you very much !

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by