What is the purpose of zero function?

23 次查看(过去 30 天)
Why zero function is introduced here ?
thank you

采纳的回答

Steven Lord
Steven Lord 2023-2-23
The zeros function is used there to preallocate the array. Rather than having to find a new block of memory to store the results and copy the existing elements to that new block of memory each time you add a new element to the array in the loop, we assign a block of memory large enough to hold all the elements we want to add in the loop and just fill it in inside the loop.
  4 个评论

请先登录,再进行评论。

更多回答(2 个)

John D'Errico
John D'Errico 2023-2-23
Consider these two codes:
tic
x = [];
for i = 1:100000
x = [x,i];
end
toc
Elapsed time is 1.538085 seconds.
clear x
tic
x = zeros(1,100000);
for i = 1:100000
x(i) == i;
end
toc
Elapsed time is 0.009933 seconds.
Do you see that both codes produce the vector 1:100000?
Why is the first code so much slower? What happens in the first one? MATLAB is forced to allocate, and then sequentially reallocate new arrays for each pass through the loop, then completely copy the entire array to a new location. This process grows longer and longer with each pass through the loop.
The point is, you preallocate such an array to its final size, BEFORE the loop.

dpb
dpb 2023-2-23
编辑:dpb 2023-2-23
function CoherentState=CoherentStatefunc(alpha,DIM)
CoherentState=zeros(DIM,1);
for n=0:(DIM-1)
CoherentState(Hang(n),1)=power(alpha,n)*exp(-abs(alpha)*abs(alpha)/2)/sqrt(factorial(n));
end
end
To preallocate the output array CoherentState the is populated one element at a time in the for...end loop.
NOTA BENE:
Unless Hang() is a function, the function will fail because the indirect addressing vector would not be defined inside the function and if it were a vector, the attempt to address it with the zero index would also error. "MATLAB is not C"
power(alpha,n) would also have to be a function.
It appears as though the above function in true MATLAB style could be written simply as
function CoherentState=CoherentStatefunc(alpha,DIM)
n=[0:(DIM-1)].';
CoherentState(Hang(n))=power(alpha,n)*exp(-abs(alpha)*abs(alpha)/2)./sqrt(factorial(n));
end
if the two other functions were also written to handle vector inputs.
As for details on "why" preallocating can be important see <preallocating-arrays> in the documentation...
  2 个评论
Abu Zar
Abu Zar 2023-2-24
Thank you very much,but why the two function hang and power is introduced here in this one function,(i already have the hang fuction),wht this cohstste is defined in this way.
dpb
dpb 2023-2-24
"...why the two function hang and power is introduced here in this one function,"
Purely a choice of the author of the code in how they chose to factor it.
A prime reason for using a function for what is a relatively short piece of code is that the result is needed more than once in the application so having the shorthand reference to it simplifies the higher level code.
That's particularly important/useful if there is a need to make a change in that result -- if the above line were scattered all around the code, one would have to be able to ensure one found and fixed every instance; in the function, it's only in the one place so the change will be automatically reflected everywhere.
That's a generic reason; another for the specific code is that as noted above, the way the function is written is indicative of a relatively inexperienced MATLAB coder having written it -- they had enough experience to know to preallocate when assigning to an array element-by-element, but not enough "time in grade" yet to recognize that there was no need for the looping construct at all; one could use the internally vectorized operations of MATLAB and thereby get the benefits thereof on the one assignment.
If the author had recognized that and the result is not needed but in the lone location that calls this function, then it might as well have been written as the one line in the calling routine instead, yes.
We don't know the overall code structure to be able to answer whether a different factorization might be better or not with just the one routine in isolation.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by