I need to optimize my script. A thing I could do is preallocating, however the size of my output cell is unknown so my question is if it is worth it.
the output of the script is X, the size of X is not known.
Now I have:
X={};
*my script
X=nxn cell
If the output cell has the size 2x2 for example, can I do something like this:
not sure if it will increase the speed of my script
X=cell(999);
*my script
X=999x999 cell
*somehow reduce the size of X
X=2x2 cell

댓글 수: 5

the size is not known. Since X is an output of several set of input it is impossible for me to preallocate an exact size
Stephen23
Stephen23 2021년 4월 12일
편집: Stephen23 2021년 4월 12일
@Joel Schelander: I am not expecting an exact size (otherwise the point of your question is moot). Most likely you can estimate the order of magnitude of how many cells you might expect (e.g. if you are processing image data, then the number of pixels places an upper limit on how many distinct objects can be represented in that image.
If we have an idea of the order of magnitude, then we can tailor our answers to help you better.
It makes a difference if your cell array is of the order one hundred cells vs one hundred million cells.
The size of X will increase to the magnitude 100 million I think. Right now Im just working with a very small sample to optimize the script.
10 millions by 10 million cell?
I don't believe any existing HW can support that at the moment.

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

 채택된 답변

Bruno Luong
Bruno Luong 2021년 4월 12일
편집: Bruno Luong 2021년 4월 12일

0 개 추천

If the size is not known, a good way is the to grow the preallocation array exponentially.
Rather than the assigment
X{i,j} = value;
Call
X = gassign(X,i,j,value);
where gassign implemented as follow
function X = gassign(X, i, j, val)
[m,n] = size(X);
if (i > m) || (j > n)
gidx = @(s,k) max(max(2*s,s+1),k);
X(gidx(m,i),gidx(n,j)) = {[]};
end
X{i,j} = val;
end
Then when finish truncate X to remove not filled cells.
You can start the script with X initialized as
X = {};

추가 답변 (1개)

KSSV
KSSV 2021년 4월 12일

0 개 추천

You can try to initilize them as
X = cell([],[]) ;
You can check the timing using tic , toc.

제품

릴리스

R2017b

태그

질문:

2021년 4월 12일

댓글:

2021년 4월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by