Info
此问题已关闭。 请重新打开它进行编辑或回答。
Group data by 2 condiitions
3 次查看(过去 30 天)
显示 更早的评论
Hi everyone I have data like this
A Type1 10
A Type2 10
A Type1 25
B Type2 12
B Type1 36
C Type1 45
C Type3 17
C Type2 40
and want to group them in a matrix like this
A B C
Type1 35 36 45
Type2 10 12 40
Type3 0 0 17
where the numbers are the sum of coresponding numbers. Can anyone help me with this?
0 个评论
回答(3 个)
Azzi Abdelmalek
2012-10-18
编辑:Azzi Abdelmalek
2012-10-18
A={'A Type1 10' 'A Type1 10' 'A Type1 25' 'B Type2 12' 'B Type1 36' 'C Type1 45' }
B=cellfun(@(x) regexp(x,' ','split'),A,'un',0)'
M=cellfun(@(x) x(1),B)
type=cellfun(@(x) x(2),B)
Num=cellfun(@(x) x(3),B)
1 个评论
Azzi Abdelmalek
2012-10-18
编辑:Azzi Abdelmalek
2012-10-18
A={'A Type1 10'
'A Type2 10'
'A Type1 25'
'B Type2 12'
'B Type1 36'
'C Type1 45'
'C Type3 17'
'C Type2 40 '}
B=cellfun(@(x) regexp(x,' ','split'),A,'un',0)
M=cellfun(@(x) x(1),B)
type=cellfun(@(x) x(2),B)
Num=cellfun(@(x) x(3),B)
M1=unique(M)
type1=unique(type)
out=cell(numel(type1),numel(M1))
for k=1:numel(M)
ii=find(ismember(type1,type(k)))
jj= find(ismember(M1,M(k)))
out(ii,jj)=Num(k);
end
out=[['/' M1']; type1 out ]
0 个评论
Sean de Wolski
2012-10-18
I don't know how you have your data stored but if you can get it into the format below (either with xlsread or whatever) then you can use accumarray
letter = cellstr(('AAABBCCC')');
Type = {'Type1','Type2','Type1','Type2','Type1','Type1','Type3','Type2'}';
value = [10 10 25 12 36 45 17 40];
%Unique values
[uL,~,lidx] = unique(letter);
[uT,~,Tidx] = unique(Type);
M = accumarray([Tidx lidx],value); %build matrix
If you have the Statistics Toolbox then you can build a dataset that completely resembles your goal:
DS = mat2dataset(M,'VarNames',uL','ObsNames',uT')
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!