MATLAB Answers

How do I unwrap a cell of arrays without a loop or cellfun?

15 ビュー (過去 30 日間)
Benjamin Siegl
Benjamin Siegl 2018 年 9 月 12 日
コメント済み: dpb 2018 年 9 月 12 日
I need a function such as padcat.m for matrices to stack them vertically. I allow the function inputs to be cells of arrays and/or arrays. The number is not limited. The naive attempt is divided in a header and a lower part.
function [S] = stackmatrix_si(varargin)
ii = cellfun(@(x) ischar(x),varargin);
if any(ii)
fllr = str2double(varargin(find(ii,1)));
fllr = 0;
A = varargin(~ii); % Save only matrices and cells
if any(cellfun(@(x) iscell(x),A))
A = [A{:}];
Lower part with for-loop:
ii = [0 , 0;cellfun(@(x) size(x,1),A) , cellfun(@(x) size(x,2),A)];
S = fllr*ones(sum(ii(:,1)),max(ii(:,2))); % Init. S
for jj=1:length(A)
S(sum(ii(1:jj,1))+1:sum(ii(1:jj+1,1)), ... % Put in the values
1:ii(jj+1,2)) = A{jj};
Since the cell inputs are not limited in their number of contained arrays, I am wondering how to first unwrap arrays in cells and stack them without using a for-loop or a cellfun for that.
The lower part was substituted without using a for-loop:
ii = [cellfun(@(x) size(x,1),A) , cellfun(@(x) size(x,2),A)];
A = cellfun(@(x) x(:),A.','UniformOutput',0);
A = cat(1,A{:});
S = ones(max(ii(:,2))+1,sum(ii(:,1)));
S(sub2ind(size(S),repelem(ii(:,2).',ii(:,1).')+1,1:size(S,2))) = fllr;
S = S(1:end-1,:);
S(S==1) = A; % Init. S
S = S.';
Unfortunately, for that I need the row
A = cellfun(@(x) x(:),A.','UniformOutput',0);
And that's why I am asking how to get one big (row or column) list of all numbers contained in all fields in a cell.

  1 件のコメント

dpb 2018 年 9 月 12 日
Only by some version of above; ML doesn't support the syntax of


回答 (0 件)


Community Treasure Hunt

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

Start Hunting!

Translated by