Try x=copy(a). Matlab automatically copies the array if it's written to.

On Sun, Aug 31, 2014 at 1:52 PM, Alex Hollingsworth
<[email protected]> wrote:
> Hi Everyone,
>
> I cannot figure out if there is an error in Julia or (more likely) in my
> code. I have a matrix A, which contains some NaN values and I would like to
> create a copy of it that is the same except that I replace the NaN values
> with 0's. I would also like to do this without altering the original matrix.
> I have tried two different approaches, both of which have the same whacky
> result. Where no matter what I do, the original values seem to be altered in
> A. My results would ideally look like:
>
> a =[1 2 3; 4 5 NaN] and x=[1 2 3; 4 5 0]
>
> Please let me know where my error is or if this is some oddity of julia's
> handling of NaN's. The same logic of code works perfectly in Matlab, so I'm
> really confused as to what the error is.
>
> Thanks!!
>
> Method 1:
> a=[1 2 3; 4 5 NaN]
> x=a
>
> for m=1:size(x,1)
> for l=1:size(x,2)
>   isnan(x[m,l]) ? x[m,l]=0 :  x[m,l]=x[m,l]
> end
> end
>
> Result:
>
> julia> a
>
> 2x3 Array{Float64,2}:
>
>  1.0  2.0  3.0
>
>  4.0  5.0  0.0
>
> julia> x
>
> 2x3 Array{Float64,2}:
>
>  1.0  2.0  3.0
>
>  4.0  5.0  0.0
>
>
> Method 2:
>
> julia> a=[1 2 3; 4 5 NaN]
>
> 2x3 Array{Float64,2}:
>
>  1.0  2.0    3.0
>
>  4.0  5.0  NaN
>
>
> julia> x=a
>
> 2x3 Array{Float64,2}:
>
>  1.0  2.0    3.0
>
>  4.0  5.0  NaN
>
>
> julia> x[isnan(x)]=0
>
> 0
>
>
> julia> x
>
> 2x3 Array{Float64,2}:
>
>  1.0  2.0  3.0
>
>  4.0  5.0  0.0
>
>
> julia> a
>
> 2x3 Array{Float64,2}:
>
>  1.0  2.0  3.0
>
>  4.0  5.0  0.0

Reply via email to