# What is the purpose of zero function?

5 views (last 30 days)

Show older comments

Why zero function is introduced here ?

thank you

##### 0 Comments

### Accepted Answer

Steven Lord
on 23 Feb 2023

##### 3 Comments

### More Answers (2)

John D'Errico
on 23 Feb 2023

Consider these two codes:

tic

x = [];

for i = 1:100000

x = [x,i];

end

toc

clear x

tic

x = zeros(1,100000);

for i = 1:100000

x(i) == i;

end

toc

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.

##### 0 Comments

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

dpb
on 24 Feb 2023

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

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!