Group characters and form a matrix in Matlab

后端 未结 3 1342
太阳男子
太阳男子 2021-01-27 12:02

I have 26 characters A to Z, I group 4 characters together and separate the following 4 characters by a space which is like this:

abcd efgh ijkl mnop qrst uvwx y         


        
3条回答
  •  花落未央
    2021-01-27 12:34

    Code ( an approach with vec2mat) -

    %// Input
    input_str = 'abcdefghijklmnopqrstuvwxyz' %// Input
    
    %// Parameters
    group_numel = 4;
    num_groups_per_row = 2;
    
    str1 = vec2mat(input_str,group_numel)
    str2 = [str1,repmat(' ',size(str1,1),1)]
    output_str = vec2mat(str2,(group_numel+1)*num_groups_per_row)
    

    Code run -

    >> input_str
    input_str =
    abcdefghijklmnopqrstuvwxyz
    >> output_str
    output_str =
    abcd efgh 
    ijkl mnop 
    qrst uvwx 
    yz       
    

提交回复
热议问题