Answer the question
In order to leave comments, you need to log in
How to expand a matrix by value python?
There is a longitude matrix, you need to add values around each element.
Found the numpy.pad tool , but it adds elements around the matrix. And you need around each value, expanding the matrix.
Answer the question
In order to leave comments, you need to log in
As an option:
import numpy as np
A = [[1,2], [3,4]]
def pad_el(el):
return np.stack(
(
np.zeros(3),
np.pad([el], 1, mode='constant'),
np.zeros(3)
), axis=1)
out_array = []
for i in range(len(A)):
row = []
for j in range(len(A[i])):
if len(row) == 0:
row = pad_el(A[i][j])
else:
row = np.block([row, pad_el(A[i][j])])
if len(out_array) == 0:
out_array = row
else:
out_array = np.vstack((out_array, row))
print(out_array)
[[0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 2. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 3. 0. 0. 4. 0.]
[0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 2. 0. 0. 3. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 3. 0. 0. 4. 0. 0. 5. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 6. 0. 0. 7. 0. 0. 8. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question