'how to repeat a numpy vector to create an array with the rest of array being zeros
I would like the create the following numpy array, based on the following vector
e = numpy.array([1,0,0,0,0,0])
a = [ [e, 0, ---, 0],
[0, e, ---, 0],
-
-
[0, 0, ---, e]]
(Note: The 0 in this array is thus a zero vector and not scalar)
and thus;
a = [ [1,0,0,0,0,0, 0,0,0,0,0,0, ---, 0,0,0,0,0,0],
[0,0,0,0,0,0, 1,0,0,0,0,0, ---, 0,0,0,0,0,0],
-
-
[0,0,0,0,0,0, 0,0,0,0,0,0 ---, 1,0,0,0,0,0]]
The solutions does not have to make use of e. The structure of the first array (based on e) is due the underlying linear algebra of the problem I'm tackling.
I have looked at tile and repeat from numpy. However, I was not able to create a with these functions. Ideally, I would like to use a numpy function as speed is quite important for my implementation.
EDIT: e is an numpy array and not a python list
EDIT: added some extra information
Solution 1:[1]
Initially the suggestion from 'Michael Szczesny', was the one I ended up using. However, I found out as well that the Kronecker product is the mathematical operation which I was looking for.
From this StackOverflow answer it seems(/seemed) that the SciPy implementation works better than the NumPy one. This answer is quite old (2013). However, I do not have enough reputation to ask a followup question.
Maybe someone else would benefit from this information
Solution 2:[2]
I can offer a solution with a single for loop:
import numpy as np
# Define the sizes of the array.
a, b = 4, 6
matrix = np.zeros(shape=(a*b,b), dtype=int)
for i in range(b):
matrix[i*a,i] = 1
print(matrix.T)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | rammelmueller |
