Issue
Consider the following (Nx1)
array:
a = [[1]
[2]
[3]
[4]
[5]]
How to generate an (NxN)
array from a
? For e.g. N = 5
:
a = [[1 1 1 1 1]
[2 2 2 2 2]
[3 3 3 3 3]
[4 4 4 4 4]
[5 5 5 5 5]]
Solution
If you want to copy the values, you can use np.repeat
:
>>> np.repeat(a, len(a), 1)
array([[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4],
[5, 5, 5, 5, 5]])
Else, you should perform a broadcast and wrap a
with a view using np.broadcast_to
:
>>> np.broadcast_to(a, (len(a),)*2)
array([[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4],
[5, 5, 5, 5, 5]])
Answered By - Ivan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.