Yves
Yves

Reputation: 12371

julia: how to declare a multi-dimensional array

I'm working with Julia and now I need to use a type named TimeArray in the package of TimeSeries.

Here is the constructor of TimeArray:

TimeArray(timestamp::Vector{Date{ISOCalendar}}, values::Array{T,N}, colnames::Vector{ASCIIString})

So I do a test here:

dts = [Date("2015-01-06"), Date("2015-01-07")]
vls = [[1, 2] [3, 4]]
cnms = ["v1", "v2"]
ta = TimeArray(dts, vls, cnms)

It works very well.

Now I have some arrays like this:

[1, 2]
[3, 4]

It means that I should create the vls above with these arrays.

I tried like this:

v1 = [1, 2]
v2 = [3, 4]
vls = []
push!(vls, v1)
push!(vls, v2)

However I can't create any TimeArray with the vls here. I got this error:

column names must match width of array

I also printed the two vls.
The first one is:

[1 3
 2 4]

The second one is:
Any[[1,2],[3,4]]

So I think I must construct the first vls, instead of the second one but I don't know how.

Upvotes: 1

Views: 504

Answers (1)

Nils Gudat
Nils Gudat

Reputation: 13800

The problem is that your push!() approach does not produce a 2x2 matrix, but a Vector{Any,2} instead. Thus, size(vls,2) is 1, which does not match length(cnms), which is 2.

You might be looking for hcat(v1,v2) instead?

Upvotes: 4

Related Questions