代码之家  ›  专栏  ›  技术社区  ›  Diamadis

如何在Matlab中拆分单元格1x1?

  •  0
  • Diamadis  · 技术社区  · 8 年前

    我的目标是:

    1. 在任何“a”旁边添加1到5“a”。

    2. 在任何“g”旁边添加一个随机数“g”。

    3. 在任何“t”旁边添加一个随机数“t”。

      例如,我有“atcatcaaca”。我的目标是让它像:“aaaattcccaattccaaaaaaaaaccccccaa”

    我的想法是取值单元格,并以某种方式将其拆分为一个矩阵:

    代码为:

    filename = fullfile(matlabroot,'virus_nucleotitde2.dat');
    
    Z = readtable(filename);
    
    S = table2cell(Z);
    
    num_rows = size (Z,1);
    num_cols = size (Z,2);
    for i=1:1:num_rows
       for j=1:1:num_cols
        B = S(i,j);
        a=0;
        c=0;
        g=0;
        t=0;
    
    
    B{1} = num2cell(B{1});
    
    n = randi(6);  % Random number between 1 and 6
    B{1} = strrep(B{1} , 'a' , repmat('a' , 1, n));
    
    n = randi(11);  % Random number between 1 and 11
    B{1} = strrep(B{1} , 'c' , repmat('c' , 1, n));
    
    n = randi(11); 
    B{1} = strrep(B{1} , 'g' , repmat('g' , 1, n));
    
    n = randi(11); 
    B{1} = strrep(B{1} , 't' , repmat('t' , 1, n));
    
        end
    
    end
    
    3 回复  |  直到 5 年前
        1
  •  0
  •   Zep    8 年前

    牢房内有一个 char 可以使用花括号访问:

     S = {'ataggatag'};
     B = S{1};
     disp(B)
    

    strrep 是你的朋友:

    n = randi(6);  % Random number between 1 and 6
    B = strrep(B , 'a' , repmat('a' , 1, n));
    
    n = randi(11);  % Random number between 1 and 11
    B = strrep(B , 'c' , repmat('c' , 1, n));
    
    n = randi(11); 
    B = strrep(B , 'g' , repmat('g' , 1, n));
    
    n = randi(11); 
    B = strrep(B , 't' , repmat('t' , 1, n));
    

    S{1} = B;
    disp(S)
    

    注意,我使用6作为“a”的最大数量,因为 串替换 将替换原来的a,而不是按您的要求在其旁边添加字母。

    OP编辑后,以下是您的解决方案:

    S = {'ataggatag'};
    
    num_rows = size (S,1);
    num_cols = size (S,2);
    
    for i=1:1:num_rows
       for j=1:1:num_cols
            n = randi(6);  % Random number between 1 and 6
            S{i,j} = strrep(S{i,j} , 'a' , repmat('a' , 1, n));
    
            n = randi(11);  % Random number between 1 and 11
            S{i,j} = strrep(S{i,j} , 'c' , repmat('c' , 1, n));
    
            n = randi(11); 
            S{i,j} = strrep(S{i,j} , 'g' , repmat('g' , 1, n));
    
            n = randi(11); 
            S{i,j} = strrep(S{i,j} , 't' , repmat('t' , 1, n));
        end
    
    end
    
    disp(S)
    
        2
  •  0
  •   Jepessen    8 年前

    已经是这样了。。 'a string' 是一个字符数组,因此为了将其转换为单元格数组,需要使用通常的 num2cell

    >> name_in_1x1_cell_array{1} = 'ataggatag'
    
    name_in_1x1_cell_array = 
    
        'ataggatag'
    
    >> name_in_1x1_cell_array{1} = num2cell(name_in_1x1_cell_array{1})
    
    name_in_1x1_cell_array = 
    
        {1x9 cell}
    

    您还可以直接访问字符,例如,您可以循环字符串的每个字符并显示它:

    name = 'some name';
    for i = 1 : length(name)
      disp(name(i));
    end
    
        3
  •  0
  •   jvz    8 年前

    strrep 这很简单,因为它也在单元阵列上运行:

    cell_string = {'atcatcaaca'};
    
    % add a's
    cell_string = strrep(cell_string, 'a', repmat('a',1,5));
    
    % add c's
    cell_string = strrep(cell_string, 'c', repmat('c',1,10));
    
    % add t's
    cell_string = strrep(cell_string, 't', repmat('t',1,randi(10)));
    

    repmat 用于复制字符。