计算Matlab中的重复组合

时间:2014-06-29 13:42:15

标签: matlab match cell

让我们考虑以下情况,例如我们有细胞矩阵A:

A:
 A(:,1)A(:,2) A(:,3)   A(:,4)    A(:,5)        
'CM'  '00118''000151' '19970303''19970729'
'RO'  '01356''043605' '19970212''19970401' 
'CM'  '01464''000151' '19970121''19970218'
'RO'  '01356''043605' '19970119''19970313' 
'CM'  '00118''043605' '19970114''19970219' 
'CM'  '00118''000151' '19970523''19970728' 

在前三列中,字符串和值组合在一起并沿矩阵重复。

我会创建一个新专栏,

REP = num2cell(REP);
A(:,end+1) = REP;

对应于每个匹配A(:,1)的A(:,3)的重复次数。例如,此示例的输出为:

Output for A:
A(:,1) A(:,2) A(:,3)   A(:,4)    A(:,5)   REP        
'CM'  '00118''000151' '19970303''19970729''3'
'RO'  '01356''043605' '19970212''19970401''2'   
'CM'  '01464''000151' '19970121''19970218''3' 
'RO'  '01356''043605' '19970119''19970313''2' 
'CM'  '00118''043605' '19970114''19970219''1' 
'CM'  '00118''000151' '19970523''19970728''3'

2 个答案:

答案 0 :(得分:1)

如果您尝试这样做怎么办:

    clear all
    clc

    A = cell(6,6);
    % I replicate only the relevant part of your cell array.
    A(:,1) = {'CM' 'RO' 'CM' 'RO' 'CM' 'CM'};
    A(:,3) = {'000151' '043605' '000151' '043605' '043605' '000151'}; 


    % Concatenate strings to easily compare them afterwards.
    ConcatString = cell(size(A,1),1);
    for i = 1:size(A,1)    
        ConcatString{i} = strcat(A{i,1},num2str(A{i,3}));    
    end

    REP = zeros(size(A,1),1);

   for i = 1:size(A,1)   

   Match = strcmp(ConcatString{i}, ConcatString) % Get logical array, '1' represents match.   

   NumRep = size(find(Match ==1),1) % Number of repetitions

   REP(Match ==1) = NumRep % Enter # of repetitions in REP

   end
    A(:,6) = num2cell(REP);

    disp(A)

给了我这个:

'CM'    []    '000151'    []    []    [3]
'RO'    []    '043605'    []    []    [2]
'CM'    []    '000151'    []    []    [3]
'RO'    []    '043605'    []    []    [2]
'CM'    []    '043605'    []    []    [1]
'CM'    []    '000151'    []    []    [3]

它有点粗糙,但你可以用我的数据填充空白。

答案 1 :(得分:1)

对于这两列中的每一列,使用unique将字符串转换为唯一的数字标签。然后计算每个标签组合的重复次数sparseaccumarray也可以使用):

[~, ~, uu1 ] = unique(A(:,1)); %// get unique labels for column 1
[~, ~, uu3 ] = unique(A(:,3)); %// get unique labels for column 3
R = full(sparse(uu1, uu3, 1)); %// count repetitions of each combination
REP = R(sub2ind(size(R), uu1, uu3)); %// result as a column vector
A(:,end+1) = mat2cell(num2str(REP), ones(1,numel(REP))); %// attach result to A

在您的示例中,假设输入为

A = {'CM'    '00118'    '000151'    '19970303'    '19970729'    
     'RO'    '01356'    '043605'    '19970212'    '19970401'    
     'CM'    '01464'    '000151'    '19970121'    '19970218'    
     'RO'    '01356'    '043605'    '19970119'    '19970313'    
     'CM'    '00118'    '043605'    '19970114'    '19970219'    
     'CM'    '00118'    '000151'    '19970523'    '19970728'};

代码生成

REP =
     3
     2
     3
     2
     1
     3

A = 
    'CM'    '00118'    '000151'    '19970303'    '19970729'    '3'
    'RO'    '01356'    '043605'    '19970212'    '19970401'    '2'
    'CM'    '01464'    '000151'    '19970121'    '19970218'    '3'
    'RO'    '01356'    '043605'    '19970119'    '19970313'    '2'
    'CM'    '00118'    '043605'    '19970114'    '19970219'    '1'
    'CM'    '00118'    '000151'    '19970523'    '19970728'    '3'