1

Given a vector, for example

my_list=[1, 2, 3]

how to expand each entry to a new matrix (seen as a multidimensional list or, more likely, a numpy array)?

For example, in the case of matrix being a numpy array of size 2x2 matrix, the output, expanded_my_list, would be:

 [[[1, 1], [1, 1]], [[2, 2], [2, 2]], [[3, 3], [3, 3]]]

or as a numpy array:

array([[[1, 1],
    [1, 1]],

   [[2, 2],
    [2, 2]],

   [[3, 3],
    [3, 3]]])

where expanded_my_list.shape is (3,2,2).

cerebrou
  • 5,353
  • 15
  • 48
  • 80
  • 3
    You second snippet is not valid python. Do you mean to write three nested lists? – Brian61354270 Feb 27 '20 at 17:01
  • 1
    _Given a vector, for example_ It's important to be precise, that's a plain Python list. _E.g. in the case of matrix.shape to be 2x2 matrix:_ Where does the `shape` attribute come from? What's _matrix_ ? Please clarify things. – AMC Feb 27 '20 at 17:10
  • Corrected, thanks. – cerebrou Feb 27 '20 at 17:14
  • _Expanding a vector to a new size Ask Question_ ... _Given a vector, for example_ ... _how to expand each entry to a new matrix? E.g. in the case of matrix.shape to be 2x2 matrix:_ ? – AMC Feb 27 '20 at 17:18
  • I don't think the _or as a numpy array:_ makes sense here, since the previous sentence already implies that the "matrix" is a NumPy ndarray. – AMC Feb 27 '20 at 17:19
  • _my_list.shape is (3,2,2)._ It isn't, it's _AttributeError: 'list' object has no attribute 'shape'_. – AMC Feb 27 '20 at 17:20
  • 1
    Ultimately none of this matters, since this question is essentially a duplicate of https://stackoverflow.com/questions/1550130/cloning-row-or-column-vectors. – AMC Feb 27 '20 at 17:22

3 Answers3

1
my_list = [1, 2, 3]
[[[e] * 2 for _ in range(2)]  for e in my_list]

output:

[[[1, 1], [1, 1]], [[2, 2], [2, 2]], [[3, 3], [3, 3]]]
kederrac
  • 16,819
  • 6
  • 32
  • 55
1

One solution may be:

for i in range(len(my_list)):
    expanded[:, i] = my_list[i].expand_as(matrix[:, i])
cerebrou
  • 5,353
  • 15
  • 48
  • 80
0

You could use numpy.tile(). By creating additional axis via np.newaxis and repeating the elements of the list along these axis you can create your wanted result:

import numpy as np

lst = [1, 2, 3]
arr = np.array(lst)

arr2 = np.tile(arr[:, np.newaxis, np.newaxis], reps=(1, 2, 2))
# Output:
# array([[[1, 1],
#         [1, 1]],
#        [[2, 2],
#         [2, 2]],
#        [[3, 3],
#         [3, 3]]])

lst2 = arr2.tolist()  # If a nested list is required 

Or more general:

arr = np.array([[1, 2],
                [3, 4]])

expanded_shape = (3, 4)

arr2 = np.tile(arr.reshape(arr.shape + (1,)*len(expanded_shape)), 
               reps=(1,)*arr.ndim + expanded_shape)
# Output, shape (2, 2, 3, 4)
# array([[[[1, 1, 1, 1],
#          [1, 1, 1, 1],
#          [1, 1, 1, 1]],
#         [[2, 2, 2, 2],
#          [2, 2, 2, 2],
#          [2, 2, 2, 2]]],
#        [[[3, 3, 3, 3],
#          [3, 3, 3, 3],
#          [3, 3, 3, 3]],
#         [[4, 4, 4, 4],
#          [4, 4, 4, 4],
#          [4, 4, 4, 4]]]])
scleronomic
  • 4,392
  • 1
  • 13
  • 43