Groups 215 of 99+ julia-users › mapping to colums of a matrix 6 posts by 5 authors Tamas Papp 4/4/15 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 Tim Holy 4/4/15 Try the cat, hcat, vcat family of functions. --Tim - show quoted text - René Donner 4/7/15 Unfortunately the documentation is not really great yet, but https://github.com/rened/FunctionalData.jl#computing-map-and-friends-details is a collection of functions that could be used for this, with optional in-place operations and parallel processing. It is basically a generalization of the pattern in your code: allocate result, and fill by iterating over the input. You can then write your task like this: julia> f(x,n) = x.*(1:n) f (generic function with 1 method) julia> @p map 1:4 f 4 4x4 Array{Int64,2}: 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16 The second line is just syntax sugar for map(1:4, x->f(x,4)) (https://github.com/rened/FunctionalData.jl#pipeline-syntax-details) It basically works on a "lists-of-items" concept, trying to keep data in compact memory representations. In this case the map turns the list of scalars (the unit range) into a list of column vectors (the resulting matrix). - show quoted text - David Gold 4/7/15 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? - show quoted text - Milan Bouchet-Valat 4/7/15 Re: [julia-users] Re: mapping to colums of a matrix Le mardi 07 avril 2015 à 10:56 -0700, David Gold a écrit : > 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. Actually, comprehensions use type inference to choose the type of the array. But it doesn't work well when non-const global variables are involved. If you declare v as const you get an Array{Int}. You could also wrap this inside a function and make v an argument. Anyway, even inside functions, you're safer specifying the element type. For details, see https://github.com/JuliaLang/julia/issues/7258 Regards - show quoted text - David Gold 4/7/15 Re: [julia-users] Re: mapping to colums of a matrix Very good to know. Thank you for the clarification! - show quoted text -