MATLAB: Combine each row of matrix into a single vector within a cell array

vectorization

Hello,
I have a big mxn matrix with m>100,000 and n=1800. Each row of this matrix contains measurement values for a specific point in time. For further processing of the data, I would like to create a mx1 cell array containing the rows of the matrix. Is there a possibility achieving this without a loop?
Here is my code, using a for loop:
A = rand(100000,1800);
b = cell(size(A,1), 1);
for ii = 1:size(b,1)
b{ii,1} = A(ii,1:end);
end

Best Answer

This sounds like a job for num2cell.
A = rand(100000,1800);
tic
b = cell(size(A,1), 1);
for ii = 1:size(b,1)
b{ii,1} = A(ii,1:end);
end
toc
Replace "1:end" by the faster ":" :
clear b
tic
b = cell(size(A,1), 1);
for ii = 1:size(b,1)
b{ii,1} = A(ii, :);
end
toc
Compare with num2cell:
clear b
tic
b = num2cell(A, 2);
toc
Even faster without the overhead of num2cell, but operating in columnwise order:
tic
A = A.';
b = cell(size(A,2), 1);
for ii = 1:size(b,1)
b{ii,1} = A(:, ii).';
end
toc
Timings under R2016b, i7, 8GB RAM:
Elapsed time is 3.165280 seconds. Original
Elapsed time is 2.672808 seconds. 1:end -> :
Elapsed time is 2.386299 seconds. num2cell
Elapsed time is 1.717735 seconds. columnwise processing