Dear Contributers,
There is a " for loop" in my program and Matlab gives me a suggestion to consider "Preallocating" for speed. I want to learn and do that. However, I don't know where to start to learn it. Of course, I looked the documents in mathworks but I found a few documents in there. I can read all of them but I don't want to rush headlong into this topic. Please, I am asking your ideas where I should start from those documents;
By the way, My code is;
for k1 =1: length(X)
for k2 =1: length(X_inv)
DE(k1,k2)= hypot(X(k1)-X_inv(k2), Y(k1)-Y_inv(k2));
end
end
And Matlab suggested me to preallocate the DE
I am not sure whether this question is appropriate for here or not. If it is not okay, accept my apologies.

 채택된 답변

John D'Errico
John D'Errico 2015년 12월 2일
편집: John D'Errico 2015년 12월 3일

8 개 추천

When you grow an array incrementally as you are doing with DE, MATLAB is forced to reallocate the entire array at EVERY iteration, copying over the entire mess just to add ONE element. This will cause the operation to get slower, and the time required will grow quadratically. SO it will get SLOW.
You know in the end EXACTLY how large DE will be. So just add ONE extra line before the loop.
DE = zeros(length(X),length(X_inv));
This essentially creates the array in advance, filling it with zeros. It need no longer reallocated at each iteration, because it will no longer need to change size at every step.
Much of the time, preallocation is not needed. MATLAB is not a language where you need to initialize/allocate every array. Variables that are scalars, and will remain so are never an issue. It is only when an array is dynamically grown that preallocation is important. A problem arises when you don't know the final size of your array. Even there, there are tricks that one can do. I posted a submission to the FEX long ago that allows the user to store information in a form that can be more efficiently grown, then at the end, you can unpack the object into a regular array.

댓글 수: 4

Ender Rencuzogullari
Ender Rencuzogullari 2015년 12월 3일
편집: Ender Rencuzogullari 2015년 12월 3일
Program is ,now, working extremely fast. Marvellous! Thank you. I want to learn the logic of that much more now. I will read that file as soon as possible soon. Thank you again, sir.
John D'Errico
John D'Errico 2015년 12월 3일
I'm always happy to see & help someone wanting to learn, as opposed to see someone wanting their homework done for them. :)
SINDU GOKULAPATI
SINDU GOKULAPATI 2021년 5월 17일
편집: Stephen23 2021년 5월 17일
hey john im facing a similar issue , for context below is my code
a=csvread('D:\sem6\PROJECT\hygdata.csv',1,5,[1,5,5068,7]); %reading x,y,z
n=5068;
//r = zeros(1,n) %error:Unable to perform assignment because brace indexing is not supported for variables of this type.
for i=1:n
r{i} = transpose(a(i,:)); %mztrix [x y z]
end
what is the alternative here?
"what is the alternative here?"
Preallocate the array using the correct class:
r = cell(1,n)

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

추가 답변 (1개)

Madison Schossow
Madison Schossow 2018년 3월 8일

0 개 추천

try doing length(X):-1:1

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

질문:

2015년 12월 2일

댓글:

2021년 5월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by