문제

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!

도움이 되었습니까?

해결책

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

arr = Array(Float64, dims...)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top