As a Matlab convert who had the same realisation recently .... +1 for this thread!
On Wednesday, 7 October 2015 23:49:33 UTC+1, Patrick Kofod Mogensen wrote: > > Maybe I'm wrong but I think it's different in Matlab. If a variable exists > already, it will try to reuse it. I know Julia is not Matlab, and I don't > want it to be, but all scientific programming experience I have is from > Matlab, so I just assumed. Which is stupid. > > I totally get your explanation, I just hadn't thought about it. I've been > doing a whoooole lot of unnecessary assignments instead of setindexing! :) > > On Wednesday, October 7, 2015 at 6:16:18 PM UTC-4, Simon Danisch wrote: >> >> Well that's how assignment usually works. >> The *[:]* is something else and is redirecting you to setindex!. >> *x[:] = 42* is the same as *setindex(a, 42, :)*, which is the same as >> *setindex(a, >> 42, 1:length(a))* >> >> So it's assigning to the indexes of that vector, which means it's reusing >> it. >> Maybe have a look at array_indexing >> <http://julia.readthedocs.org/en/latest/manual/arrays/#indexing> ? >> >> Am Mittwoch, 7. Oktober 2015 22:25:00 UTC+2 schrieb Patrick Kofod >> Mogensen: >>> >>> So I asked a question over at >>> https://github.com/lindahua/Devectorize.jl/issues/48#issuecomment-146307811 >>> and it seems that I have got something completely wrong. >>> >>> It seems that the following >>> >>> index = rand(8000) >>> phat = zeros(8000) >>> >>> phat = 1./(1+exp(-index)) >>> >>> binds the output of the calculation on the rhs to a _new_ vector phat, >>> not the original vector. This really means I have misunderstood >>> preallocation/re-using memory in Julia completely! @blakejohnson suggests >>> that I use >>> >>> phat[:] = 1./1+(exp(-index)) >>> >>> instead. As this puts the calculations in the already preallocated >>> vector phat. >>> >>> Question 1) Can I learn more about this in the documentation ? I'm >>> having a hard time finding anything, probably because [:] is hard to use as >>> a search phrase, and I'm not sure what concepts to search for. >>> >>> >>> >>> I often wrap my variables in a type, but this seems like an extremely >>> bad idea in combination with devec. I have taken Blake Johnsons examples >>> from github, and added a few >>> https://gist.github.com/pkofod/fb6c4b8ffcca1a056363 >>> >>> Question 2) What is it that makes test4() and test6() run so slowly? >>> test1() and test3() seems to perform equivalently fast. I use this kind of >>> setup all the time, and I am surprised that this method allocates so much >>> memory. >>> >>> Bad news: I've been doing Julia ALL wrong! >>> Good news: Time to learn some more! >>> >>
