Would 

copy!(b, a);

work?


On Wednesday, March 11, 2015 at 4:29:09 PM UTC, Isaiah wrote:
>
> To assign new values in-place to the existing 'b' array, change this line:
>
> b=copy(a)
>
> to 
>
> b[:] = a
>
> I've also tried with do loops, but this causes a segmentation fault.
>
>
> Segfaults in pure-Julia code (no user-defined ccalls) are almost always 
> considered a bug -- could you post an example, or ideally file an issue on 
> github with a test case? (please also include the output of versioninfo())
>
> On Wed, Mar 11, 2015 at 5:19 AM, Peter Drummond <[email protected] 
> <javascript:>> wrote:
>
>> How do you pass two arrays to a function so that the function can copy 
>> one to the other, and return the values in place? Of course, a=b won't 
>> work, but neither does a=copy(b). See the following example:
>>
>> julia> function copytest!(a,b)
>>            b=copy(a)
>>            println ("copytest: a,b ",a,b)
>>            nothing
>>        end
>> copytest! (generic function with 1 method)
>>
>> julia> a=ones(2,2)
>> 2x2 Array{Float64,2}:
>>  1.0  1.0
>>  1.0  1.0
>>
>> julia> b=zeros(2,2)
>> 2x2 Array{Float64,2}:
>>  0.0  0.0
>>  0.0  0.0
>>
>> julia> copytest!(a,b)
>> copytest: a,b [1.0 1.0
>>  1.0 1.0][1.0 1.0
>>  1.0 1.0]
>>
>> julia> println ("copytest: a,b ",a,b)
>> copytest: a,b [1.0 1.0
>>  1.0 1.0][0.0 0.0
>>  0.0 0.0]
>>
>> The first println shows that b WAS changed inside the copytest function. 
>> The second println shows that it WASN'T changed on return. I've also tried 
>> with do loops, but this causes a segmentation fault.
>>
>
>

Reply via email to