MATLAB: How to rearrange cell array with the grouped sequence

rearrange

I have a cell array in this format:
21 3 '1'
31 2 '1 1'
25 1 '2 2 1 1'
How can I rearrange the cell array to:
21 3 1
31 2 1
31 2 1
25 1 2
25 1 2
25 1 1
25 1 1
Thank you!

Best Answer

C is your input array as I've understood it; D is the output matrix.
% Produce input array
C = {21 3 '1'
31 2 '1 1'
25 1 '2 2 1 1'};
% Convert strings in column 3 to vectors
Cv = cellfun(@(s)str2double(strsplit(s)).',C(:,3),'UniformOutput',false);
% Replicate the rows of C(:,[1,2]) for each value in Cv
C2 = arrayfun(@(i)repmat([C{i,[1,2]}],numel(Cv{i}),1),1:size(C,1),'UniformOutput',false)';
% Combine C2 with Cv values
D = [cell2mat(C2), cell2mat(Cv)];
Result
D =
21 3 1
31 2 1
31 2 1
25 1 2
25 1 2
25 1 1
25 1 1