MATLAB Answers


group every ten cell array

Asked by pasta pontikaki on 18 May 2019 at 21:52
Latest activity Edited by pasta pontikaki on 20 May 2019 at 16:47
Hi all i have a cell array 1x180 and i want to group by every 10 cells
For example i want the elements from 1 cell till 10 get grouped,11:21, 22:32 etc..
So the new cell should be 1x18
How can i do this?


Sign in to comment.

1 Answer

Answer by Adam Danz
on 18 May 2019 at 22:09
Edited by Adam Danz
on 18 May 2019 at 22:10
 Accepted Answer

c = cell(1,180); % the original 1x180 cell array
newCell = mat2cell(c,1,repmat(10,1,18)); %the new nested cell array


horizontally, remember i have a cell array with 180 vectors with different size
Adam Danz
on 19 May 2019 at 15:45
Yeah, you can still concatenate them vertically but you'd need to pad the shorter vectors. Here are both methods:
Concatenate horizontally
newCellMerge = cellfun(@(x)[x{:}],newCell, 'UniformOutput', false);
Concatenate vertically, pad with NaN values at the end of short vectors
newCellMerge = cell(size(newCell));
for i = 1:length(newCell)
len = cellfun(@length, newCell{i});
cellPad = cellfun(@(x,y)padarray(x',y,NaN,'post')',newCell{i}, num2cell(max(len)-len),'UniformOutput',false)';
newCellMerge{i} = cell2mat(cellPad);
please sir with all duly respect could you please answer in this question if you know
I will realy appreciate this it is a matter of life or death for my homework

Sign in to comment.