Code: Alles auswählen
import numpy as np
A = np.arange(7*5, dtype=np.int32).reshape(7, 5)
A
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34]])
addVals = np.stack( ( A, A ) )
addVals[1] = addVals[0] + 100
addVals
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[ 10, 11, 12, 13, 14],
[ 15, 16, 17, 18, 19],
[ 20, 21, 22, 23, 24],
[ 25, 26, 27, 28, 29],
[ 30, 31, 32, 33, 34]],
[[100, 101, 102, 103, 104],
[105, 106, 107, 108, 109],
[110, 111, 112, 113, 114],
[115, 116, 117, 118, 119],
[120, 121, 122, 123, 124],
[125, 126, 127, 128, 129],
[130, 131, 132, 133, 134]]])
addVals.shape
(2, 7, 5) # Ist dir bewusst und ist das gewollt, dass addVals diesen Shape hat und so aussieht ?
mask = np.zeros_like(A)
mask
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
mask[1:-1, 1:-1] = A[1:-1, 1:-1] % 2 # for decision, which value from addVals
mask
array([[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0]])
# mask hat einen shape (7,5) und sieht so aus, ist das (Muster) so richtig und gewollt?
B = addVals[mask]
# was soll diese Zeile bewirken, wie soll deiner Meinung nach das Ergebnis aussehen ?
# Das Ergebnis hat hier schon den shape (7, 5, 7, 5)
B.shape
(7, 5, 7, 5)
A_w = A + B # but resultls in array with shape (7, 5, 7, 5)
A_w.shape
(7, 5, 7, 5)