Does using cells cause dramatically slow down the code?

 Accepted Answer

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

In my code there are many matrices coming out of loops. But all of them just for one iteration and I should store them for all iterations. (I havent add the outer loop which is for iterations) I think I have two options:
1) Rearranging the code to add every matrices to first one in every iteration:
iter1
a=[1 2 3
3 4 5]
iter2
a=[1 2 3
3 4 5
4 5 6
5 6 7]
iter3
a=[1 2 3
3 4 5
4 5 6
5 6 7
6 7 8
1 5 7 ]
2) Storing them in a cell.
cell{1,1}= a=[1 2 3
3 4 5]
cell{1,2}= a= [4 5 6
5 6 7]
I have two many matrices like 'a' and they more complex.
I hope I can clarify.
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
Looks like you might not be preallocate the cell array

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!