converting a cell array of doubles in a matrix
Show older comments
I have a variable A which looks like this:
1×2 cell array
{1×9 double} {1×9 double}
I want to convert it to a matrix, i.e, the first row of matrix should be what's inside the A{1} and second row of matrix should be what's inside A{2}. A is a cell, but A{1} and A{2} are doubles. How can i do this?
1 Comment
Stephen23
on 19 Jun 2018
vertcat(A{:})
Accepted Answer
More Answers (1)
OCDER
on 19 Jun 2018
A = {rand(1,10000), rand(1,10000)};
B = vertcat(A{:});
1 Comment
OCDER
on 19 Jun 2018
cell2mat is certainly the right function for converting cell arrays to matrices while preserving the MxN dimension of the cell array. However, vertcat is a built-in function (meaning super optimized) and will run ~2x faster for your case.
A = {rand(1,10000), rand(1,10000)};
t1 = timeit(@() vertcat(A{:}), 1);
t2 = timeit(@() cell2mat(A), 1);
fprintf('cell2mat(A'') time is %0.2f times slower than vertcat(A{:})\n', t2/t1);
Categories
Find more on Matrices and Arrays 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!