Reputation: 33
I have a set of 9 different arrays, all n by n in size. I need to combine them element wise in order to yield a specific array.
Ex.: Given a set of 9 equally sized arrays:
a1 = np.array([[11, 12, 13], [14, 15, 16], [17, 18, 19]])
a2 = np.array([[21, 22, 23], [24, 25, 26], [27, 28, 29]])
a3 = np.array([[31, 32, 33], [34, 35, 36], [37, 38, 39]])
a4 = np.array([[41, 42, 43], [44, 45, 46], [47, 48, 49]])
a5 = np.array([[51, 52, 53], [54, 55, 56], [57, 58, 59]])
a6 = np.array([[61, 62, 63], [64, 65, 66], [67, 68, 69]])
a7 = np.array([[71, 72, 73], [74, 75, 76], [77, 78, 79]])
a8 = np.array([[81, 82, 83], [84, 85, 86], [87, 88, 89]])
a9 = np.array([[91, 92, 93], [94, 95, 96], [97, 98, 99]])
The desired result would be
b = np.array([[11, 21, 31, 12, 22, 32, 13, 23, 33],
[41, 51, 61, 42, 52, 62, 43, 53, 63],
[71, 81, 91, 72, 82, 92, 73, 83, 94],
[14, 24, 34, 15, 25, 35, 16, 26, 36],
[44, 54, 64, 45, 55, 65, 46, 56, 66],
[74, 84, 94, 75, 85, 95, 76, 86, 96],
[17, 27, 37, 18, 28, 38, 19, 29, 39],
[47, 57, 67, 48, 58, 68, 49, 59, 69],
[77, 87, 97, 78, 88, 98, 79, 89, 99]])
So the first row of array b is made up of the first rows of a1, a2, and a3, combined element wise.
The second row of array b is made up of the first rows of a4, a5, and a6, combined element wise.
The third row is made up of the first rows of a7, a8, and a9, combined element wise.
And then the same pattern continues for the rest of the rows in a1-a9.
This needs to work for any size of the a1-a9 arrays, as in the arrays being n by n in size. I've tried tinkering with np.concatenate, zip, and np.einsum, all with no luck.
Upvotes: 3
Views: 104
Reputation: 1216
As for time speed (used timeit.repeat
on 10000 iterations to get that), from the best to the worst:
np.stack
+ a.reshape(3, 3, 3, 3).transpose(2, 0, 3, 1).reshape(9, 9)
: 9.9e-2 svstack
+ hstack
+ reshape
: 1.6e-1 sUpvotes: 1
Reputation: 59731
EDIT: Generalized to different array sized and number of arrays:
import numpy as np
def combine_arrays(arrays):
arrays = np.asarray(arrays)
n, p, q = arrays.shape
s = int(round(np.sqrt(n)))
arrays = arrays.reshape(s, -1, p, q)
return arrays.transpose(2, 0, 3, 1).reshape(s * p, -1)
a1 = np.array([[11, 12, 13], [14, 15, 16], [17, 18, 19]])
a2 = np.array([[21, 22, 23], [24, 25, 26], [27, 28, 29]])
a3 = np.array([[31, 32, 33], [34, 35, 36], [37, 38, 39]])
a4 = np.array([[41, 42, 43], [44, 45, 46], [47, 48, 49]])
a5 = np.array([[51, 52, 53], [54, 55, 56], [57, 58, 59]])
a6 = np.array([[61, 62, 63], [64, 65, 66], [67, 68, 69]])
a7 = np.array([[71, 72, 73], [74, 75, 76], [77, 78, 79]])
a8 = np.array([[81, 82, 83], [84, 85, 86], [87, 88, 89]])
a9 = np.array([[91, 92, 93], [94, 95, 96], [97, 98, 99]])
print(combine_arrays([a1, a2, a3, a4, a5, a6, a7, a8, a9]))
# [[11 21 31 12 22 32 13 23 33]
# [41 51 61 42 52 62 43 53 63]
# [71 81 91 72 82 92 73 83 93]
# [14 24 34 15 25 35 16 26 36]
# [44 54 64 45 55 65 46 56 66]
# [74 84 94 75 85 95 76 86 96]
# [17 27 37 18 28 38 19 29 39]
# [47 57 67 48 58 68 49 59 69]
# [77 87 97 78 88 98 79 89 99]]
a1 = np.array([[11, 12], [14, 15]])
a2 = np.array([[21, 22], [24, 25]])
a3 = np.array([[31, 32], [34, 35]])
a4 = np.array([[41, 42], [44, 45]])
print(combine_arrays([a1, a2, a3, a4]))
# [[11 21 12 22]
# [31 41 32 42]
# [14 24 15 25]
# [34 44 35 45]]
You can do that by reshaping and transposing:
import numpy as np
a1 = np.array([[11, 12, 13], [14, 15, 16], [17, 18, 19]])
a2 = np.array([[21, 22, 23], [24, 25, 26], [27, 28, 29]])
a3 = np.array([[31, 32, 33], [34, 35, 36], [37, 38, 39]])
a4 = np.array([[41, 42, 43], [44, 45, 46], [47, 48, 49]])
a5 = np.array([[51, 52, 53], [54, 55, 56], [57, 58, 59]])
a6 = np.array([[61, 62, 63], [64, 65, 66], [67, 68, 69]])
a7 = np.array([[71, 72, 73], [74, 75, 76], [77, 78, 79]])
a8 = np.array([[81, 82, 83], [84, 85, 86], [87, 88, 89]])
a9 = np.array([[91, 92, 93], [94, 95, 96], [97, 98, 99]])
a = np.stack([a1, a2, a3, a4, a5, a6, a7, a8, a9])
a = a.reshape(3, 3, 3, 3).transpose(2, 0, 3, 1).reshape(9, 9)
print(a)
# [[11 21 31 12 22 32 13 23 33]
# [41 51 61 42 52 62 43 53 63]
# [71 81 91 72 82 92 73 83 93]
# [14 24 34 15 25 35 16 26 36]
# [44 54 64 45 55 65 46 56 66]
# [74 84 94 75 85 95 76 86 96]
# [17 27 37 18 28 38 19 29 39]
# [47 57 67 48 58 68 49 59 69]
# [77 87 97 78 88 98 79 89 99]]
Upvotes: 5
Reputation: 13397
Try this one:
>>> import numpy as np
>>> np.hstack([
np.vstack([a1.ravel(), a2.ravel(), a3.ravel()]).T.reshape(3, -1),
np.vstack([a4.ravel(), a5.ravel(), a6.ravel()]).T.reshape(3, -1),
np.vstack([a7.ravel(), a8.ravel(), a9.ravel()]).T.reshape(3, -1)
]).reshape(9,9)
array([[11, 21, 31, 12, 22, 32, 13, 23, 33],
[41, 51, 61, 42, 52, 62, 43, 53, 63],
[71, 81, 91, 72, 82, 92, 73, 83, 93],
[14, 24, 34, 15, 25, 35, 16, 26, 36],
[44, 54, 64, 45, 55, 65, 46, 56, 66],
[74, 84, 94, 75, 85, 95, 76, 86, 96],
[17, 27, 37, 18, 28, 38, 19, 29, 39],
[47, 57, 67, 48, 58, 68, 49, 59, 69],
[77, 87, 97, 78, 88, 98, 79, 89, 99]])
Upvotes: 1