On Thu, Jun 2, 2016 at 3:01 PM, Yichao Yu <yyc1...@gmail.com> wrote: > On Thu, Jun 2, 2016 at 2:28 PM, Kaela Martin <kaelama...@gmail.com> wrote: >> I have a function that computes the unit vector and replaces the input with >> that unit vector. However, when I have the input as part of a larger array, >> it doesn't replace that part of the array. >> >> For example, if the vector I want to make a unit vector is R = [1. 2.; 3. >> 4.; 5. 6.], the function unit!(R[:,1]) (listed below) should replace the > > R[:, 1] does not return a view, sth like `sub(R, :, 1)` should work.
For reference, if you think you have an opinion on what `R[:, 1]` should return, you can read https://github.com/JuliaLang/julia/issues/13157 and https://github.com/JuliaLang/julia/pull/9150 and various linked discussions. > >> first column of R with its unit vector. The current code is not. What am I >> doing wrong? >> >> Thanks for the help! >> >> function unit!{T}(V::AbstractArray{T}) >> v=mag(V) >> >> for ii in 1:3 >> V[ii] = V[ii]/v >> end >> >> if v == 0 >> V[1] = 1 >> V[2] = 0 >> V[3] = 0 >> end #no divide by 0 >> >> return v >> end >> >> function mag{T}(R::AbstractArray{T}) >> >> R=sqrt(dot(R,R)) >> >> end