The benefit of mapreduce is that it's not constructing two different arrays - one that is an array of arrays, and one that is a 2d array. Instead, it hcats as it builds. But there might, possibly, be an issue with having to hcat the whole array one at a time (I don't know if mapreduce is smart enough to notice that hcat is transitive*) - it's probably worth testing. I also think some dedicated function for this purpose would be helpful, since it should be possible to pre-allocate the array.
And the reason why my code does it that way is that you've got "exact" outputting a column vector - at some point, you'll have to convert to a row vector for them, if you want it to have 2 columns and many rows. In which case, the neatest would probably be this: mapreduce(i->exact(i)',vcat,linspace(0,10,100)) This will make it vcat row vectors instead of hcatting column vectors. * Maybe it's worth having a special type of function, TransitiveFunction, that automatically converts f(f(a,b),c) into f(a,b,c), or something, for the purposes of operations like reduce? On Wednesday, 28 October 2015 02:52:21 UTC+10, Gabriel Gellner wrote: > > Sadly that wants to make a matrix of two long rows ;) like the hcat(...). > So needs the transpose as well ... maybe this is the way? > Thanks for opening my eyes to mapreduce though! > > On Tuesday, 27 October 2015 09:43:59 UTC-7, Glen O wrote: >> >> One relatively neat way to do this is >> >> mapreduce(exact,hcat,linspace(0,10,100)) >> >> On Wednesday, 28 October 2015 02:38:56 UTC+10, Gabriel Gellner wrote: >>> >>> Okay sorry tab seems to send ... >>> >>> I am trying my to figure out the Julian way to create a table of values >>> (matrix) from a function that returns multiple values. As this is really >>> thinking about the problem as a function that generates the rows of the >>> table it feels super awkward to do this in Julia currently. For example, >>> lets say I have a function of the form: >>> >>> function exact(t) >>> yout = zeros(2) >>> yout[1] = 3.0*exp(t) - 2.0*exp(t) >>> yout[2] = exp(t) + 2.0*exp(t) >>> yout >>> end >>> >>> then what i want is a matrix of these solutions so my first thought is >>> to do >>> >>> esol = [exact(t) for t in linspace(0, 10, 100)] >>> hcat(esol...)' >>> >>> is this the idiomatic solution? >>> >>> Is there a better way to do this? How do people generally deal with >>> Array or Arrays. Feels weird to me currently. >>> >>> Gabriel >>> >>> >>> On Tuesday, 27 October 2015 09:31:22 UTC-7, Gabriel Gellner wrote: >>>> >>>> I am trying my to figure out the Julian way to create a table of values >>>> (matrix) from a function that returns multiple values. As this is really >>>> thinking about the problem as a function that generates the rows of the >>>> table it feels super awkward to do this in Julia currently. For example, >>>> lets say I have a function of the form: >>>> >>>> function exact_solution(t) >>>> >>>>
