0

This question looks a bit like this:
List of 2D arrays with different size into 3D array
and even more like this:
Turning list of 2D tensors with different length to one 3D tensor,

but either I don't get it or the small differences that both dimensions in the 2d lists in my list can vary and that I have to pad my tensor to (1st dim, max 2nd dim, max 3rd) do change things enough to warrant another question.

Given a list of 2D elements I want to:

  1. find the max of each of these dimensions and create a 3D tensor with dim 0 = length of list, dim 1 = max length of dim 0 of each element in the list, dim 2 = max length of dim 1 of each element in the list.
  2. create a tensor from the list that is padded to these dimensions with zeros.

Example: Given:

[ [ [0, 1, 2]     , [8, 0]            ], 
  [ [12, 13, 6, 7], [12]  , [23, 11] ] ]

Output:

[ [ [0, 1, 2, *0*], [8, 0, *0*, *0*   ], [*0*, *0*, *0*, *0*]  ], 
  [ [12, 13, 6, 7], [12, *0*, *0*, *0*], [23, 11, *0*, *0*  ]  ] ]

I was guessing this would be the algorithm, but it is missing a part I don't even have a guess for, I would like some certainty before I try to implement by guess, I have a suspicion there's some simpler way like some variation of cat() and reshape() that would do this.

  1. Do I use a loop to create a list with the length of each list in the list given?
  2. Get the max value of this list. Set this as the max of the 2nd dim, dim = 1.
  3. Create another list to hold the lengths of the lists/arrays forming the 3rd dim, i.e. the lists in the lists in the list.
  4. Assign this to the max of the 3rd dimension.
  5. Now I loop through the length of the list over these max of the 2nd and 3rd dim and in the 2nd dim somehow figure out that I am at the end of that list (???)
  6. add empty lists until the end of that loop, then
  7. loop through the max of the 3rd dim and assign the value if it exists, somehow figure out if I am at the end of the list even if the list is empty (???)
  8. assign 0 for the remainder.
DerekG
  • 3,555
  • 1
  • 11
  • 21
mh57
  • 3
  • 1

0 Answers0