MATLAB Answers

Does using cells cause dramatically slow down the code?

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

  0 Comments

Sign in to comment.

Accepted Answer

Guillaume
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?

  4 Comments

Show 1 older comment
Guillaume
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();
end
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)

Tags


Translated by