This solution worked. Thanks so much!
On Thursday, June 2, 2016 at 12:02:05 PM UTC-7, Yichao Yu wrote: > > On Thu, Jun 2, 2016 at 2:28 PM, Kaela Martin <[email protected] > <javascript:>> 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. > > > 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 >
