如何添加额外的列,使用matlab计算单元格数组中每个字符的出现次数

时间:2012-10-31 09:22:15

标签: matlab

我需要编写一个代码,为cellarray GO的第四列的每个单元格中的字符出现次数添加额外的列:

GO:
      'GO:0008150'    [1]    [1]    'a'
      'GO:0016740'    [2]    [2]    'b'
      'GO:0006412'    [2]    [2]    'b'
      'GO:0016787'    [2]    [3]    'c'
      'GO:0006810'    [2]    [4]    'd'
      'GO:0016787'    [3]    [3]    'c'
      'GO:0004672'    [3]    [3]    'c'
      'GO:0016779'    [3]    [3]    'c'
      'GO:0005215'    [3]    [3]    'c'
      'GO:0006810'    [3]    [4]    'd'
      'GO:0004386'    [3]    [4]    'd'
      'GO:0003774'    [3]    [4]    'd'
      'GO:0016298'    [3]    [4]    'd'
      'GO:0016192'    [3]    [5]    'e'
      'GO:0006412'    [3]    [2]    'b'
      'GO:0005215'    [3]    [3]    'c'
      'GO:0006810'    [4]    [4]    'd'
      'GO:0004386'    [4]    [4]    'd'
      'GO:0003774'    [4]    [4]    'd'
      'GO:0016298'    [4]    [4]    'd'
      'GO:0030533'    [4]    [4]    'd'
      'GO:0030533'    [4]    [4]    'd'
      'GO:0016192'    [4]    [5]    'e'

得到的GO cellarray应如下(从0值开始):

GO:     
  'GO:0008150'    [1]    [1]    'a'   '0'
  'GO:0016740'    [2]    [2]    'b'   '0'
  'GO:0006412'    [2]    [2]    'b'   '1'
  'GO:0016787'    [2]    [3]    'c'   '0'
  'GO:0006810'    [2]    [4]    'd'   '0'
  'GO:0016787'    [3]    [3]    'c'   '1'
  'GO:0004672'    [3]    [3]    'c'   '2'
  'GO:0016779'    [3]    [3]    'c'   '3'
  'GO:0005215'    [3]    [3]    'c'   '4'
  'GO:0006810'    [3]    [4]    'd'   '1'
  'GO:0004386'    [3]    [4]    'd'   '2'
  'GO:0003774'    [3]    [4]    'd'   '3'
  'GO:0016298'    [3]    [4]    'd'   '4'
  'GO:0016192'    [3]    [5]    'e'   '0'
  'GO:0006412'    [3]    [2]    'b'   '2'
  'GO:0005215'    [3]    [3]    'c'   '5'
  'GO:0006810'    [4]    [4]    'd'   '5'
  'GO:0004386'    [4]    [4]    'd'   '6'

我尝试了以下代码,但无论如何它都不起作用:

x3=[];  % for saving the resulted numbers 
z=0:length(GO);  % will take the numbers from this matrix
z=z';
 for j=1:length(num2alph)
     for k=1:length(GO)
 for i=1:length(GO)
         if isequal(GO{i,4},num2alph{j})
                   x3{i}=z(k);
    else
     end
     end
     end
 end
   x3=x3';
   GO1=[GO x3];

其中cellarray num2alph(将它与GO数组的第四列进行比较以构建数字列)是:

  'a'          
  'b'        
  'c'    
  'd'   
  'e'    
  'f'    
  'g'   
  'h'   
  'i'    
  'j'    
  'k'  

我还需要将最后两列彼此映射到同一个单元格中,例如:

         'a'   '0'  ===>  a0
         'b'   '2'  ===>  b2

任何建议

THX

2 个答案:

答案 0 :(得分:3)

这应该可以解决问题:

un = unique(GO(:,4));

results = zeros(size(GO(:,4)));
for ii = 1:numel(un)

    inds = strcmp(GO(:,4), un(ii));
    count = max(0, cumsum(inds)-1);

    results(inds) = count(inds);
end


GO = [GO  num2cell(results)]

答案 1 :(得分:2)

用户定义的函数

cellhist可以真正成为你的朋友。它似乎可以确切地解决您的需求。