Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put a character in between every character of all strings in a cell-array

I have input-cell = {'ABCD', 'ABD', 'BCD'}. How can I put the operator < into the strings in input-cell?

The expected output should be:

 output-cell = {'A<B<C<D', 'A<B<D', 'B<C<D'}
like image 657
kgk Avatar asked Feb 09 '23 20:02

kgk


1 Answers

To insert a fixed character (<) between the characters of each string in a cell array: you can use regexeprep as follows:

input_cell = {'ABCD', 'ABD', 'BCD'};                        %// input cell array
c = '<';                                                    %// character to be inserted
output_cell = regexprep(input_cell, '.(?=.)', ['$0' c]);    %// output cell array

Result:

output_cell = 
    'A<B<C<D'    'A<B<D'    'B<C<D'
like image 93
Luis Mendo Avatar answered Feb 11 '23 15:02

Luis Mendo