On Wed, Dec 25, 2013 at 7:20 PM, Sheehan Olver <[email protected]>wrote:

> Suppose f(k) returns a Array{Float64,1}, and I want to construct an
> Array{Float64,2} whose kth row is f(k).  What's the best way to do this?
>  Right now I'm using
>
>      mapreduce(x->x,vcat,[f(k) for k = 1:n])
>
> where the point of the mapreduce is to convert the Array{Any,1} to
> Array{Float64,2}.  But it seems like it should be simpler.
>

First, it seems like f(k) returns a row vector (which is a 1xn matrix of
type Array{Float64, 2}), rather than a column vector of type Array{Float64,
1}.  Otherwise, your mapreduce code above would produce one long
Array{Float64, 1} vector.

If f(k) actually returns a column vector, you could do

    hcat([f(k) for k = 1:n]...)'

This concatenates the column vectors horizontally, then takes a transpose
to put each return value in a row.  In my tests, that returns an
Array{Float64, 2}, even though the input is an Array{Any,1}.

Since Julia uses column major order, you might consider *not* taking the
transpose, and leaving the return values in successive columns, rather than
successive rows.  But that depends on what you're actually doing.

Cheers,

    Kevin

P.S. I'm testing on a recently compiled version from master.  If you're
using the current release version (v0.2), there's a small chance you might
have different results, although I'm pretty sure it should be the same.

Reply via email to