Hi I have a 19 x 3
matrix, however I want to make it 513 x 3
matrix.
So i just need to maintain my 19 rows and the remaining zeroes or ones to make up 513
is this possible?
CodePudding user response:
You can use numpy.pad()
In your case, if you want your 19x3 values in the center of the matrix, you should use:
import numpy as np
padded_matrix = np.pad(your_matrix, ((247, 247), (0, 0)))
247
comes from (513-19)/2
EDIT: If instead, you want to keep the values in the first 19 rows and pad the matrix at the bottom, change the parameters:
padded_matrix = np.pad(your_matrix, ((0, 513-19), (0, 0)))
CodePudding user response:
You could do it like this (check [NumPy]: Assigning values to indexed arrays and [NumPy]: Broadcasting for more details):
>>> >>> import numpy as np >>> >>> >>> rows_inner, cols_inner = 19, 3 >>> rows_outer, cols_outer = 513, 3 >>> >>> inner = np.arange(rows_inner * cols_inner).reshape(rows_inner, cols_inner) >>> inner.shape, inner.dtype ((19, 3), dtype('int32')) >>> inner array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11], [12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23], [24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35], [36, 37, 38], [39, 40, 41], [42, 43, 44], [45, 46, 47], [48, 49, 50], [51, 52, 53], [54, 55, 56]]) >>> >>> >>> outer = np.zeros((rows_outer, cols_outer), dtype=np.int32) >>> outer.shape, outer.dtype ((513, 3), dtype('int32')) >>> outer array([[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]]) >>> >>> >>> outer[0:rows_inner,:] = inner # !!! Simple Assignment !!! >>> >>> outer.shape, outer.dtype ((513, 3), dtype('int32')) >>> outer array([[0, 1, 2], [3, 4, 5], [6, 7, 8], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]])
CodePudding user response:
Use append
on axis=0
:
arr2 = np.zeros((513-19, 3))
new_arr = np.append(arr1, arr2, axis=0)