Issue
I have a 5x5 ones matrix:
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
and I want to fill it with this one dimension array:
[0.72222515 1.25003225 0.11767353 0.16121767 0.27926356 0.78412963
0.08371721 1.77536532 1.67636045 0.55364691]
like:
[[0.72222515 1.25003225 0.11767353 0.16121767 0.27926356]
[0.78412963 0.08371721 1.77536532 1.67636045 0.55364691]
[[0.72222515 1.25003225 0.11767353 0.16121767 0.27926356]
[0.78412963 0.08371721 1.77536532 1.67636045 0.55364691]
[[0.72222515 1.25003225 0.11767353 0.16121767 0.27926356]
What can I do in python?
Solution
Probably we can do that with a loop, like this... Not sure if this is the best way...
import numpy as np
arr1 = np.ones((5,5))
arr2 = np.array([1,2,3,4,5,6,7,8,9,10])
arr2 = np.reshape(arr2, (-1, 5))
for i in range(0,len(arr1)):
if i % 2 == 0:
arr1[i] = arr2[0]
else:
arr1[i] = arr2[1]
# Output of arr1
array([[ 1., 2., 3., 4., 5.],
[ 6., 7., 8., 9., 10.],
[ 1., 2., 3., 4., 5.],
[ 6., 7., 8., 9., 10.],
[ 1., 2., 3., 4., 5.]])
Answered By - Sachin Kohli
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.