Yes, this is sort of what I was looking for, there is one problem though. 
f(i,j) will be called K times but there is only need for one call.
Maybe this is a little better (no redundant calls to f()), but there still 
is a lot of unnecessary copying, maybe it will be optimized away?

tmp = [f(i, j) for i in 1:n, j in i:m]
[tmp[i,j][k] for k in 1:K, i in  1:n, j in 1:m]

Are there better ways?

Den måndagen den 24:e mars 2014 kl. 23:12:03 UTC+1 skrev Gunnar Farnebäck:
>
> [f(i,j)[k] for k in 1:K, i in 1:n, j in i:m]
>
> Den måndagen den 24:e mars 2014 kl. 15:07:49 UTC+1 skrev Linus Mellberg:
>>
>> Hi!
>>
>> I'm trying to construct a 3 dimensional array from a number of 1 
>> dimensional arrays. Essentially what i would like to do is
>>
>> a = [f(i, j) for i in 1:n, j in 1:m]
>>
>> where f(i, j) is a function that returns an array (note, f has to 
>> construct the entire array at the same time). The code above creates a 
>> 2-dimensional array of arrays, but I would like to get a 3-dimensional 
>> array with the arrays returned by f in the first dimension with i and j in 
>> the second and third dimension, hope you understand
>>
>> a[:,:,1] = [f(1,1) f(2,1) ... f(n,1)]
>> a[:,:,2] = [f(1,2) f(2,2) ... f(n,2)]
>> .
>> .
>> .
>> a[:,:,m] = [f(1,m) f(2,m) ... f(n,m)]
>>
>> f(i,j) are column arrays above.
>>
>> It can be achieved by first creating the large matrix and then filling it
>>
>> a = zeros(Int64, k, n, m)
>> for i in 1:n, j in 1:m
>>   a[:,i,j] = f(i,j)
>> end
>>
>> Is this the only way? I find it sort of ugly when its usually possible to 
>> do nice construction using comprehensions in other cases.
>>
>>

Reply via email to