Hi all i have a cell array 1x180 and i want to group by every 10 cells
conc.PNG
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?

 採用された回答

Adam Danz
Adam Danz 2019 年 5 月 18 日
編集済み: Adam Danz 2019 年 5 月 18 日

0 投票

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

5 件のコメント

pasta pontikaki
pasta pontikaki 2019 年 5 月 19 日
Thank you this works fine, but i also want to merge also all the elements, for every ten cells.
Do you know how to do this?
Adam Danz
Adam Danz 2019 年 5 月 19 日
Yeah, I can help with that. I see that each element is a row vector of varying length. Do you want to concatenate them horizontally (example 1 below) or vertically (example 2 below)?
% Example 1
a = [1 2 3 4];
b = [5 6 7 8 9 10 11];
result = [1 2 3 4 5 6 7 8 9 10 11];
% Example 2
result = [ 1 2 3 4 nan nan nan;
5 6 7 8 9 10 11 ];
pasta pontikaki
pasta pontikaki 2019 年 5 月 19 日
編集済み: pasta pontikaki 2019 年 5 月 19 日
horizontally, remember i have a cell array with 180 vectors with different size
Adam Danz
Adam Danz 2019 年 5 月 19 日
編集済み: Adam Danz 2019 年 5 月 19 日
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);
end
pasta pontikaki
pasta pontikaki 2019 年 5 月 20 日
編集済み: pasta pontikaki 2019 年 5 月 20 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by