Converting Array{Array{Float64,1},1} to Array{Float64,2} in Julia

arrays, julia

Solution

If you look at the definition of `cat` (which is the underlying function for `hcat` and `vcat`), you see that you can collect several arrays into one single array of dimension 2:

cat(2, [1,2], [3,4], [5,6])

2×3 Array{Int64,2}:
 1  3  5
 2  4  6

This is basically what you want. The problem is that you have all your output polar points in an array itself. `cat` expects you to provide them as several arguments. This is where `...` comes in.

`...` used to cause a single function argument to be split apart into many different arguments when used in the context of a function call.

Therefore, you can write

cat(2, [[1,2], [3,4], [5,6]]...)

2×3 Array{Int64,2}:
 1  3  5
 2  4  6

In your situation, it works exactly in the same way (I changed your `x` to have the points in columns):

x=rand(2,5)
cat(2, cart2pol.(view(x,1,:),view(x,2,:))...)

2×5 Array{Float64,2}:
 0.587301  0.622    0.928159  0.579749  0.227605
 1.30672   1.52956  0.352177  0.710973  0.909746

Problem

My problem is similar to the problem described earlier, with the difference that I don't input numbers manually. Thus the accepted answer there does not work for me. I want to convert the vector of cartesian coordinates to polars: ``` function cart2pol(x0, x1) rho = sqrt(x0^2 + x1^2) phi = atan2(x1, x0) return [rho, phi] end @vectorize_2arg Number cart2pol function cart2pol(x) x1 = view(x,:,1) x2 = view(x,:,2) return cart2pol(x1, x2) end x = rand(5,2) vcat(cart2pol(x)) ``` The last command does not collect Arrays for some reason, returning the output of type `5-element Array{Array{Float64,1},1}`. Any idea how to cast it to `Array{Float64,2}`?

Original source

Related problems