What is the purpose of zero function?
6 次查看（过去 30 天）
Why zero function is introduced here ?
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.
John D'Errico 2023-2-23
Consider these two codes:
x = ;
for i = 1:100000
x = [x,i];
x = zeros(1,100000);
for i = 1:100000
x(i) == i;
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.
To preallocate the output array CoherentState the is populated one element at a time in the for...end loop.
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
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...
"...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.
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!