[julia-users] Re: a simple function which changes the order of a vector (by modifying the argument)

2015-06-08 Thread bernhard
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



[julia-users] Re: a simple function which changes the order of a vector (by modifying the argument)

2015-06-08 Thread John Myles White
http://julia.readthedocs.org/en/release-0.3/manual/faq/#i-passed-an-argument-x-to-a-function-modified-it-inside-that-function-but-on-the-outside-the-variable-x-is-still-unchanged-why

http://www.johnmyleswhite.com/notebook/2014/09/06/values-vs-bindings-the-map-is-not-the-territory/

On Monday, June 8, 2015 at 9:28:16 AM UTC-7, bernhard wrote:

 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