MATLAB: How to do a group by in matlab

findgroup bysearchsum

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.

Best Answer

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]
lookfor=[10;11];
a=arrayfun(@(x) data(find(data(:,1)==x),:),lookfor,'un',0);
b=cell2mat(cellfun(@(x) [x(1) sum(x(:,2:end),1)],a,'un',0))
%or
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]
lookfor=[10;11]
for k=1:numel(lookfor)
ii=data(ismember(data(:,1),lookfor(k)),:);
res(k,:)=[ii(1,1) sum(ii(:,2:end))];
end
res