Comprehensions may be what you are looking for:
http://docs.julialang.org/en/latest/manual/arrays/#comprehensions.
For your example,
julia> v = [1, 2, 3, 4] # Set the vector v
4-element Array{Int64,1}:
1
2
3
4
julia> A = [ j.*v[i] for j in 1:4, i in 1:length(v) ] # Create a 2d array A
where A[i, j] is given by j times the ith element of v
4x4 Array{Any,2}:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
I believe that, by default, arrays created using comprehensions are of type
Any. If you want an array of a specific type -- say, integer in this case
-- you can do
A = Int[ j.*v[i] for j in 1:4, i in 1:length(v) ]
4x4 Array{Int64,2}:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
Is this what you're looking for?
On Saturday, April 4, 2015 at 11:04:26 AM UTC-4, Tamas Papp wrote:
>
> Hi,
>
> Suppose I have a function that maps an atom of type T, eg Float64, into
> Vector{T}, and takes another argument n that determines its length.
>
> What is the idiomatic/fast way of collecting the values in the columns
> of a matrix?
>
> Currently I am using this:
>
> @doc """Map elements of `x` into columns of a matrix using `f`.
> Result is assumed to have the same element type as `x`.""" ->
> function maptocols{T}(f,x::Vector{T},n)
> k = length(x)
> b = Array(T, n, k)
> for j = 1:k
> b[:,j] = f(x[j],n)
> end
> b
> end
>
> Eg
>
> julia> maptocols((x,n) -> x.*[1:n;], [1,2,3,4], 4)
> 4x4 Array{Int64,2}:
> 1 2 3 4
> 2 4 6 8
> 3 6 9 12
> 4 8 12 16
>
> which is OK but of course I would prefer a one-liner if there is one
> provided by the language (could not find it though).
>
> Best,
>
> Tamas
>