mat2cell sizes
2 views (last 30 days)
Show older comments
Within a function, I convert a cell array C_in to a numeric array A_in:
A_in = cell2mat(C_in);
I then perform calculations on A_in that will result in A_out with the same size as A_in. In the end, I need to convert the numeric array A_out to a cell array C_out, with the sizes of C_out being exactly the same as C_in. I know that mat2cell is used for this, but I was wondering if there is a straight-forward way to have mat2cell figure out the row and column distances automatically based on C_in. C_in is expected to have a size of {[1, n1], [1, n2], [1, n3], ...} or {[n1, 1], [n2, 1], [n3, 1], ...}, and my function should produce a C_out with identical dimensions in both cases.
0 Comments
Answers (1)
Walter Roberson
on 10 Nov 2017
cellsizes = cellfun(@size, C_in);
C_r = cellfun(@(M) M(1), cellsizes);
C_c = cellfun(@(M) M(2), cellsizes);
mat2cell(A_out, C_r, C_c)
3 Comments
Walter Roberson
on 10 Nov 2017
cellsizes = cellfun(@size, C_in, 'Uniform', 0);
if size(A_in,2) == 1
C_r = cellfun(@(M) M(1), cellsizes);
C_c = 1;
else
C_r = 1;
C_c = cellfun(@(M) M(2), cellsizes);
end
C_out = mat2cell(A_out, C_r, C_c);
See Also
Categories
Find more on Data Type Conversion 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!