How to duplicate columns in a cell array based on different values in last row

If I have a 4x2 cell array, D, which is equal to the following...
'2' '2'
'01346' '01347'
'P' 'P'
'62 63 71' '64 66'
How do I repeat the columns and split up the last row of the columns, like so...
'2' '2' '2' '2' '2'
'01346' '01346' '01346' '01347' '01347'
'P' 'P' 'P' 'P' 'P'
'62' '63' '71' '64' '66'
Any help and input would be helpful and greatly appreciated. I haven't written any specific code, but I was thinking along the lines of splitting the string of the last cells first and then trying to duplicate the columns, but I was unsure of how exactly to proceed. Thank you!

 Akzeptierte Antwort

How about this code? I hope this will be help.
% Input cell array
D = {'2' '2';
'01346' '01347';
'P' 'P';
'62 63 71' '64 66'};
Output = cell(0);
for kk1 = 1:size(D,2)
C = strsplit(D{end,kk1});
for kk2 = 1:numel(C)
Output = [Output [D(1:end-1,kk1); C(kk2)]];
end
end

Weitere Antworten (0)

Kategorien

Gefragt:

am 12 Jun. 2017

Kommentiert:

am 21 Jun. 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by