Thank you John. I can see that x[:]=x[srt] does the trick.

Am Montag, 8. Juni 2015 18:28:16 UTC+2 schrieb bernhard:
>
> All
>
> I would like to sort a vector according to a given index (or permutation). 
> I understand that the code below produces a false (last evaluation below) 
> due to the fact that the function creates a local copy of x.
> How can I modify my function such that x is in sorted differently (i.e. 
> such that the argument is modified)?
> Thank you in advance.
>
>
> x=rand(5)
> anothervector=rand(5)
>
> mypermutation=sortperm(x)
>
> function change_order!(x,srt)
>   x=x[srt]
>   nothing
> end
>
> before=copy(anothervector)
> change_order!(anothervector,mypermutation)
>
> before==anothervector[mypermutation] #false
>
> before==anothervector #true, this should be false
> before[mypermutation]==anothervector #false, this should be true
>

Reply via email to