2014-05-14 18 views
6

假設我想創建一個多維數組,其維數/尺寸在數組中指定。我想要這樣做:Julia:用尺寸數組調用Array()

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

這是不允許的。在上述情況下應該使用:

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

我不知道變暗的時間提前的長度,因此上述解決方案並不爲我工作。 除了使用一些討厭的sprintfs和eval之外,還有一個乾淨的解決方法嗎?

謝謝!

+0

您通常不會在julia中使用帶eval的sprintf。元編程有更好的選擇。 – ivarne

回答

10

一個真正有用的運營商要記住在朱莉婭是「啪」,...。在這種情況下,您只需要:

arr = Array(Float64, dims...)