'Repeat every element in a 2D array to a 2D output
I have a 2D array like this one -
import numpy as np
arr = np.array([(1,2),(4,6)])
I want this output -
arr2 = array([1,1,2,2], [4,4,6,6])
What I have so far is giving me 1D output:
arr2=[]
for i in range(len(arr)):
a = arr[i,:]
for j in range(len(a)):
arr2.append(a[j])
arr2.append(a[j])
np.array(arr2)
array([1, 1, 2, 2, 4, 4, 6, 6])
any suggestions would be highly appreciated. Thank you.
Solution 1:[1]
You can use np.repeat:
arr = np.repeat(arr, 2).reshape((2, -1))
print(arr)
Prints:
[[1 1 2 2]
[4 4 6 6]]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrej Kesely |
