I have a string : str='HDEABGCF'. How can I create the new pairs which is from the second is combined with the third elements, the fourth with fifth , the sixth is combined with the seventh?
The expected output should be: result={'DE';'AB';'GC'}
You can abuse arrayfun
, and ensuring you start from the second index of the string array going up to the second last index in increments of 2. For each index, you'd access the string at the current index and the next index after that point, then ensure that the output is a cell array by using the uni=0
flag:
>> str='HDEABGCF';
>> result = arrayfun(@(x) str([x x+1]), 2:2:numel(str)-1, 'uni', 0);
>> result
result =
'DE' 'AB' 'GC'
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments