I have tabular data in Matlab. It can be stored either in struct or in cell array.
How to perform grouping like done with unique(A,'rows')
? It is not working for cell arrays for some reason.
Are there some approaches?
UPDATE
>> A={'Morgan', 'male', 12
'Morgan', 'female', 7
'Dottie', 'female', 5
'Dottie', 'female', 13}
A =
'Morgan' 'male' [12]
'Morgan' 'female' [ 7]
'Dottie' 'female' [ 5]
'Dottie' 'female' [13]
>> A(:,1:2)
ans =
'Morgan' 'male'
'Morgan' 'female'
'Dottie' 'female'
'Dottie' 'female'
>> B=ans;
>> unique(B,'rows')
Warning: The 'rows' input is not supported for cell array inputs.
> In cell.unique>celluniqueR2012a at 237
In cell.unique at 149
ans =
'Dottie'
'Morgan'
'female'
'male'
As you see it is not grouping by row, it is grouping by all values in bag.
UPDATE 2
The only approach I am researching is like follows
>> [cell2mat(B(:,1)) repmat(':',size(B,1),1) cell2mat(B(:,2))]
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 84)
m{n} = cat(1,c{:,n});
But it is (1) incredibly complex and (2) does not work yet.
UPDATE 3
I was looking for what is possible with statistics toolbox:
>> A={'Name', 'Gender', 'Age'; 'Ann', false, 20; 'John', true, 25; 'Peter', true, 30; 'Ann', false, 28}
A =
'Name' 'Gender' 'Age'
'Ann' [ 0] [ 20]
'John' [ 1] [ 25]
'Peter' [ 1] [ 30]
'Ann' [ 0] [ 28]
>> B=cell2dataset(A,'ReadVarNames',true)
B =
Name Gender Age
'Ann' false 20
'John' true 25
'Peter' true 30
'Ann' false 28
>> grpstats(B,{'Name','Gender'},{'numel'})
ans =
Name Gender GroupCount numel_Age
Ann_0 'Ann' false 2 2
John_1 'John' true 1 1
Peter_1 'Peter' true 1 1