How to filter and group data in matlab

3 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Walter Roberson
Walter Roberson il 29 Nov 2012
accumarray( [ICa, ICb], data.values, [], @nansum)

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by