0

我在 Matlab 中有表格数据。它可以存储在结构或单元数组中。

如何像 done with 一样进行分组unique(A,'rows')?由于某种原因,它不适用于单元阵列。

有一些方法吗?

更新

>> 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'

如您所见,它不是按行分组,而是按包中的所有值分组。

更新 2

我正在研究的唯一方法如下

>> [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}); 

但它 (1) 非常复杂,并且 (2) 还行不通。

更新 3

我一直在寻找统计工具箱的可能性:

>> 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        
4

1 回答 1

2

如果正确理解您的示例,您想要的输出是:

ans =
    'Morgan'    'male'
    'Morgan'    'female'
    'Dottie'    'female'

unique忽略元胞数组中的行。您可以通过将列拼接在一起来欺骗它。找到唯一行后,您可以使用该列表从元胞数组中获取信息。

% Use A as you defined it
B = A(:, 1:2);
C = strcat(B(:,1), ';', B(:,2)); % this gives you 'Morgan;male', etc.
[D cRow] = unique(C); % Find the unique rows in your new 1-column list

E = B(sort(cRow), :); % Find the values in the cell array given the unique rows

这给出了:

E =
    'Morgan'    'male'
    'Morgan'    'female'
    'Dottie'    'female'

如果这不是您要查找的输出,请更新您的问题。

于 2013-08-02T19:11:43.987 回答