MATLAB Answers

Does using cells cause dramatically slow down the code?

25 views (last 30 days)
Does using cells instead of matrixes make big difference?


Sign in to comment.

Accepted Answer

Guillaume on 4 Apr 2019
A cell array of scalar numbers will always be slower to process that a matrix with the same scalar numbers, yes. There's also no point in using a cell array for that since you can't perform mathematical operations directly on a cell array.
What is the context behind your question?


Show 1 older comment
Guillaume on 4 Apr 2019
With the two schemes you describe, using cell arrays and concatenating the whole lot at the end would be faster than growing a matrix.
Probably, the best scheme, assuming it's always the same size of matrix returned and that you know the number of loops beforehand, is to create a matrix the right size to start with and fill it in the loop:
numiterations = 12; %for example
outputsize = [2, 3]; %for example
result = zeros(outputsize .* [numiterations, 1]); %create a matrix the correct size
for iteration = 1:numiteration
result((1:outputsize(1)) + (numiteration-1)*outputsize(1), :) = yourfunction();
Walter Roberson
Walter Roberson on 4 Apr 2019
Looks like you might not be preallocate the cell array

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by