Base.convert(::Type{Matrix{Float64}},a::Array{Any,1})

33 views
Skip to first unread message

Eric Forgy

unread,
Feb 27, 2015, 8:16:15 PM2/27/15
to julia...@googlegroups.com
I have a question similar to this one:


I've been hanging around here for a while, but still just getting my feet wet building things in Julia. One thing I needed was to be able to parse some matrices in JSON. The result I get from JSON.parse is naturally a Vector{Any}, where each element is another Vector{Any}, but I want Matrix{Float64}, so I hesitantly did this:

import Base.convert

function convert(::Type{Matrix{Float64}},a::Array{Any,1})
  nrow = length(a)
  ncol = length(a[1])
  mat = zeros(nrow,ncol)
  for i=1:nrow
    mat[i,:] = convert(Vector{Float64},a[i])
  end
  return mat
end

Is there a better way?

Row major vs column major issues aside :)
Reply all
Reply to author
Forward
0 new messages