Re: [julia-users] Re: Constructing arrays with dim > 2
It's not obvious that there is/should be a better way to do this. Arrays can hold arbitrary objects, and can be created using the comprehension syntax. The vector returned by your function is an object; hence, it should be contained within the array, not a dimension of the array. I'm not saying that's the ideal storage format for your particular problem, I'm just saying that it's about the only way that, logically, this can work. What you started from, a custom function to handle this specific case, is only a couple lines of code. To me that seems pretty good, and of course it's reusable. I have a "Utilities.jl" on my LOAD_PATH that holds a whole bunch of short functions I personally tend to use a lot but that aren't of widespread interest. --Tim On Tuesday, March 25, 2014 01:50:56 AM Linus Mellberg wrote: > 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.
[julia-users] Re: Constructing arrays with dim > 2
Found this now... Not sure if readable, though. cat(3,[cat(2,[f(i,j) for i=1:n]...) for j=1:m]...) Den tisdagen den 25:e mars 2014 kl. 09:50:56 UTC+1 skrev Linus Mellberg: > > 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. >>> >>>
[julia-users] Re: Constructing arrays with dim > 2
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. >> >>
[julia-users] Re: Constructing arrays with dim > 2
Ok, one might be less redundant if reading the thread properly before posting. Sorry about the noise. 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. >> >>
[julia-users] Re: Constructing arrays with dim > 2
[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. > >
[julia-users] Re: Constructing arrays with dim > 2
Thinking about it, it is probably not so ... efficient. Le lundi 24 mars 2014 19:41:14 UTC+1, Antoine Chevalier a écrit : > > Hi, I guess you want a 3d array instead of a 2D array containing 1D > arrays. I tried this: > > arrayLength = 10; > > matrixCols = 10; > > matrixSlices= 10; > function arrayTest(arrayLength,i,j)singleArray = > ones(1,arrayLength)*(i+j); #each array has a unique value i+jreturn > singleArrayend > > matrix3d = [arrayTest(arrayLength,i,j)[k] for k=1:arrayLength, > i=1:matrixCols, j=1:matrixSlices ]; > matrix3d[:,1,1] > > It works fine ;) > > > > Le lundi 24 mars 2014 15:07:49 UTC+1, Linus Mellberg a écrit : >> >> 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. >> >>
[julia-users] Re: Constructing arrays with dim > 2
Hi, I guess you want a 3d array instead of a 2D array containing 1D arrays. I tried this: arrayLength = 10; matrixCols = 10; matrixSlices= 10; function arrayTest(arrayLength,i,j)singleArray = ones(1,arrayLength)*(i+j); #each array has a unique value i+jreturn singleArrayend matrix3d = [arrayTest(arrayLength,i,j)[k] for k=1:arrayLength, i=1:matrixCols, j=1:matrixSlices ]; matrix3d[:,1,1] It works fine ;) Le lundi 24 mars 2014 15:07:49 UTC+1, Linus Mellberg a écrit : > > 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. > >
[julia-users] Re: Constructing arrays with dim > 2
If you really want a 3d matrix and not a 2d array containing arrays, I would have tried this : arrayLength= 10 ; matrixCol = 10; matrixSlices= 10; function arrayTest(arrayLength,i,j) singleArray = ones(1,arrayLength)*(i+j); #each array has a unique value i+j return singleArray end matrix3d = [arrayTest(arrayLength,i,j)[k] for k=1:arrayLength, i=1:matrixCol, j=1:matrixSlices ]; matrix3d[:,1,1] Le lundi 24 mars 2014 19:25:56 UTC+1, Keith Campbell a écrit : > > This runs fine for me, running Version 0.3.0-prerelease (2014-02-28 04:44 > UTC): > > f(i,j) = [i,j] > n=3; m=2 > a = [f(i, j) for i in 1:n, j in 1:m] > > Out[91]: > > 3x2 Array{Any,2}: > [1,1] [1,2] > [2,1] [2,2] > [3,1] [3,2] > > On Monday, March 24, 2014 10:07:49 AM UTC-4, Linus Mellberg wrote: >> >> 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. >> >>
[julia-users] Re: Constructing arrays with dim > 2
This runs fine for me, running Version 0.3.0-prerelease (2014-02-28 04:44 UTC): f(i,j) = [i,j] n=3; m=2 a = [f(i, j) for i in 1:n, j in 1:m] Out[91]: 3x2 Array{Any,2}: [1,1] [1,2] [2,1] [2,2] [3,1] [3,2] On Monday, March 24, 2014 10:07:49 AM UTC-4, Linus Mellberg wrote: > > 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. > >