let's say I have a 3D array x:
x = np.arange(0, 24).reshape(4, 3, 2)
output:
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]]])
If I wanted to concat copies of this row wise, but each time I want to add 1 to the first element of inner each pair, how would I do that?
I can concat 1x
np.concatenate([x, x], axis=0)
And presumably I could do that in a loop (although there may be a better way in Numpy). But the bit I can't figure out is how to +1.
This allows me to add 1 to all the elements, but then I just have a 3x4 matrix of the values +1 and I don't have the rest of the cells!
x[:,:,0] + 1
output:
array([[ 1, 3, 5],
[ 7, 9, 11],
[13, 15, 17],
[19, 21, 23]])
Feels like this is a question that's got to have been asked before? But I can't find enough material on either artithmatic or slicing with multidimensional arrays.
Thank you
Just concatenate and modify:
a = np.concatenate([x, x], axis=0)
a[x.shape[0]:,:,0] += 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.