How to do a group by in matlab
Mostra commenti meno recenti
Hi, I have the following data:
data=[10 1 2 3; 11 4 5 6;10 0 20 30; 11 4 5 6; 12 7 8 9; 17 40 50 60]
I want to look for:
lookfor=[10;11]
and get the following result:
anwser=[10 1 22 33; 11 8 10 12]
So it's a group by...
I'm looking for a dynamic anwser, data matrix and lookfor matrix will vary and be much more bigger.
thank you in advance for your precious anwsers.
2 Commenti
Azzi Abdelmalek
il 26 Mag 2013
Modificato: Azzi Abdelmalek
il 26 Mag 2013
It's grouped by what? how did you get 1,22 and 33?
Gimpy
il 26 Mag 2013
Risposta accettata
Più risposte (2)
Andrei Bobrov
il 27 Mag 2013
[i1,i2] = ismember(data(:,1),lookfor);
d2 = data(i1,2:end);
[j1,j2] = ndgrid(i2(i1),1:size(d2,2));
anwser = [lookfor,accumarray([j1(:),j2(:)],d2(:))];
Lola Davidson
il 3 Giu 2024
0 voti
For those still stumbling on this, MATLAB now has several more functions to help with grouping workflows, including groupsummary and pivot.
For this problem, if you are expecting several different lookfor values on the same dataset, it may be faster to compute all the sums with groupsummary in one go:
[sums,grps] = groupsummary(data(:,2:end),data(:,1),"sum");
out = [grps sums]
On the other hand, if you only want to compute a small subset of the grouped sums per dataset, it may be quicker to filter down with ismember first, as others have mentioned.
idx = ismember(data(:,1),lookfor);
[sums,grps] = groupsummary(data(idx,2:end),data(idx,1),"sum");
out = [grps sums]
Categorie
Scopri di più su Scripts in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!