Re: variable initialization

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: variable initialization

Colin Macdonald-2
On 2018-06-19 08:05 AM, Luca Salardi wrote:
> i want to initialize a variable only the first time
>
> i=0;  % i want initialize this variable only first time (i don't want to
> assign value 0 in all cycles)
>
> if (i>5) expression;
>
>
> i=expression();

If this is inside a function, you can use a persistent variable.

persistent i

if (isempty(i))
   i = 0
end

...

cheers,
Colin