What is the purpose of zero function?

 Accepted Answer

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 Comments

Hi,
thank you,
can you tell me why the coherent state function is defined in this way, there is two functions hang function and power function is also used in this.
please give me liitle comment about this.
hang function is availble
Are you asking why we recommend preallocating arrays?
Let's say that you and three friends are going to a restaurant and you're each arriving separately. Consider this scenario.
You ask the staff for a table for four. Since only you are present, they instead put you at a table for 1. You order your drink and now your first friend, Alice, arrives. The staff needs to move you and your drink to a table for 2 so Alice has a place to sit. Alice orders a drink then your friend Bob arrives. The staff moves you and your drink and Alice and Alice's drink to a table for 3 so Bob has a place to sit. Finally Charlie arrives. The staff moves three people and three drinks to a table for 4 then Charlie sits.
Compare that with this scenario.
You ask the staff for a table for four. They seat you at a table for 4. You order your drink. Alice arrives, sits at the table, and orders a drink. Bob arrives, sits at the table, and orders a drink. Charlie arrives and sits down.
Note that the second scenario is much shorter and doesn't involve the word "move" at all, unlike the first one which involves 3 moves.
+1 :) Nice analogy, Steven...
Thanks a lot.

Sign in to comment.

More Answers (2)

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 on 23 Feb 2023
Edited: dpb on 23 Feb 2023
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 Comments

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.
"...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.

Sign in to comment.

Asked:

on 23 Feb 2023

Commented:

on 21 Aug 2023

Community Treasure Hunt

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

Start Hunting!