The statement `z=z+5` first creates a new vector holding `z+5`, then
assigns this vector to `z`, which is then not a reference to the argument
any more. To modify it, you can write `z[:] += 5` (or `z[:] = z+5`, or
`z[:] = z[:] + 5`). Note that these expressions will still create a new
vector with the value of `z+5`, but this new vector will then copied into
the existing vector.

-erik

On Wed, Jun 8, 2016 at 1:44 PM, Michele Giugliano <[email protected]>
wrote:

> Within a function, addressing a generic element of a vector passed to it
> results in an external modification of the vector (i.e. argument passed by
> reference not value).
>
>
> How do I obtain the same effect when using the vector operator "sum to a
> scalar" ? In short, I would like the effect of the two functions f!(z) and
> g!(z), defined below, to be the same.
>
>
> function f!(z)
>
> for i=1:length(z)
>
> z[i] = 25;
>
> end
>
> z = z + 5;
>
> end
>
>
> function g!(z)
>
> for i=1:length(z)
>
> z[i] = 30;
>
> end
>
> end
>
>
> Where is my (conceptual) mistake? I would be immensely grateful for your
> help and guidance.
>



-- 
Erik Schnetter <[email protected]>
http://www.perimeterinstitute.ca/personal/eschnetter/

Reply via email to