Now that we see the solution you were after, I note a few things:
- What we *really* ended up with, is an Array{Array{Array{Int, 1}, 1}, 1},
which is something fundamentally different than an Array{Array{Int, 1},
2}.
- If we were to try to reshape this into a matrix-like shape, there
would be a lot of unfilled positions, which won’t be good for memory usage.
(This is because you can’t have a matrix where the columns have different
lengths - neither can you have one were rows have different lengths.)
Thus, I don’t think this is a problem with list comprehensions. The problem
is rather that it’s impossible to know the final size of the array without
running the loops (*we* know that binomial(J,k) is monotonous, but the
compiler doesn’t know that. So the compiler has to run binomial(J,k) *for
every k* to know how large the largest is, so it can allocate space for the
array.
// T
On Monday, September 21, 2015 at 10:59:31 AM UTC+2, Alan Crawford wrote:
Thanks Mike - precisely what i was after.
>
> While this is a perfectly acceptable solution I wondered
> whether, following Mauro's suggestion, it was worth opening an issue in any
> case because it seems like it be nice to be able to link indexes in array
> comprehensions in a similar way to for-loops. Views?
>
>
> On Monday, 21 September 2015 09:49:57 UTC+1, Michael Hatherly wrote:
>>
>> MyArray = [[zeros(Int, k) for n = 1:binomial(J, k)] for k = 1:K]
>>
>> seems to do what you want I think. Using 2 nested 1-d comprehensions
>> instead of a single 2-d comprehension.
>>
>> — Mike
>>
>> On Monday, 21 September 2015 10:37:06 UTC+2, Alan Crawford wrote:
>>>
>>>
>>> Thanks Tomas. If I do:
>>>
>>> Y = [Array(Int64,n) for n in map(k -> binomial(J,k), 1:K)]
>>>
>>> Then Y[1] gives the desired result (i.e. Y[1][k] is a length 1 vector).
>>> However, the issue for Y[2] and above. For example, if I do Y[2][k] where
>>> k∈[1,binomial(J,2)]
>>> then i get a length 1 vector, whereas I would like length 2 vector.
>>> Similarly for Y[3][k] I would like a length 3 vector.
>>>
>>>
>>> On Monday, 21 September 2015 09:23:56 UTC+1, Tomas Lycken wrote:
>>>>
>>>> Ah.
>>>>
>>>> Maybe [Array(Int64,n) for n in map(k -> binomial(J,k), 1:K)] is what
>>>> you’re looking for?
>>>>
>>>> // T
>>>>
>>>> On Monday, September 21, 2015 at 10:18:31 AM UTC+2, Alan Crawford wrote:
>>>>
>>>> The lower case k is intentional. I didn't want such a 'large' array as
>>>>> the one created when I use K because large parts of that array would be
>>>>> redundant. Ideally, I want this array to be as small as possible,
>>>>> especially since J and K might be quite a bit larger than in the example.
>>>>>
>>>>> On Monday, 21 September 2015 09:13:53 UTC+1, Tomas Lycken wrote:
>>>>>>
>>>>>> Are you sure that’s not just a typo between k and K (note the case
>>>>>> difference)?
>>>>>>
>>>>>> This works for me:
>>>>>>
>>>>>> J=10
>>>>>> K=3
>>>>>> MyArray = [Array(Int64,k) for k in 1:K, n in 1:binomial(J,K)]
>>>>>>
>>>>>> // T
>>>>>>
>>>>>> On Monday, September 21, 2015 at 10:08:13 AM UTC+2, Alan Crawford
>>>>>> wrote:
>>>>>>
>>>>>> Hi,
>>>>>>>
>>>>>>> I'd like to be able to define an array of vectors where the number
>>>>>>> of vectors in the array is linked to the length of the vector. For
>>>>>>> example,
>>>>>>> I want to be define an array with say 10 scalars, 45 length 2 vectors,
>>>>>>> 120
>>>>>>> length 3 vectors, .... and so on. Intuitively, I thought the following
>>>>>>> code
>>>>>>> might achieve this:
>>>>>>>
>>>>>>> J=10
>>>>>>> K=3
>>>>>>> MyArray = [Array(Int64,k) for k in 1:K, n in 1:binomial(J,k)]
>>>>>>>
>>>>>>>
>>>>>>> However, it seems i cannot use k to define the number of element
>>>>>>> indexed by n.
>>>>>>>
>>>>>>> I was wondering if anyone knew how to create the desired array?
>>>>>>>
>>>>>>> Thanks
>>>>>>> Alan
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>
>>>