Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pad numpy array with fixed array as constant

I have the following code :

x = np.array([[1]])
print x
print np.lib.pad(x,(1,1),mode='constant',constant_values=[4,8])

output :

[[1]]
[[4 4 8]
[4 1 8]
[4 8 8]]

the problem is : in the constant values I want to put the new padding for example :

print np.lib.pad(x,(1,1),mode='constant',constant_values = [1,2,3,4,5,6,7,8])

and output like :

[[1,2,3]
[8,1,4]
[7,6,5]]
like image 548
samer226047 Avatar asked Sep 21 '26 09:09

samer226047


1 Answers

This is a reverse engineering of Print two-dimensional array in spiral order:

inner_array = np.array([3, 6, 7, 2])
outer_array = np.array([0, 23, 3, 5, 6, 8, 99, 73, 18, 42, 67, 88, 91, 12])

total_array = inner_array[::-1][np.newaxis]
i = 0
while True:
    s = total_array.shape[1]
    try:
        total_array = np.vstack([total_array, outer_array[i:i+s]])
    except ValueError:
        break
    try:
        total_array = np.rot90(total_array, -1)
    except ValueError:
        pass
    i += s
total_array = np.rot90(total_array, -1)
print(total_array)
like image 79
Ophir Carmi Avatar answered Sep 22 '26 23:09

Ophir Carmi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!