채택된 답변

Guillaume
Guillaume 2019년 4월 4일

1 개 추천

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

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
Thank you!
Looks like you might not be preallocate the cell array

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2019년 4월 4일

댓글:

2019년 4월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by