How do I unwrap a cell of arrays without a loop or cellfun?
7 views (last 30 days)
Show older comments
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.
Header:
function [S] = stackmatrix_si(varargin)
ii = cellfun(@(x) ischar(x),varargin);
if any(ii)
fllr = str2double(varargin(find(ii,1)));
else
fllr = 0;
end
A = varargin(~ii); % Save only matrices and cells
if any(cellfun(@(x) iscell(x),A))
A = [A{:}];
end
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};
end
end
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.';
end
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 Comment
dpb
on 12 Sep 2018
Only by some version of above; ML doesn't support the syntax of
v=[A{:}(:)]
unfortunately.
Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!