Question

Suppose I want to create a multidimensional array whose dimensions / size per dimension are specified in an array. I want to do something like this:

dims = [2,5,6] # random example, the idea is I don't know dims ahead of time
arr = Array(Float64, dims)

That is not allowed. In the above case one should use:

arr = Array(Float64, dims[1], dims[2], dims[3] )

I don't know the length of dims ahead of time, so the above solution doesn't work for me. Is there a clean workaround outside of using some nasty sprintfs and eval?

Thanks!

Was it helpful?

Solution

A really useful operator to remember in Julia is the “splat”, .... In this case you simply want:

arr = Array(Float64, dims...)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top