I'm trying to write a function that is applied to only specific columns in a numpy array and replace the input values with the new computed ones.
So for example my array looks like
array = np.arange(1,28).reshape(3,3,3)
which is:
[[[ 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]]]
now I want to apply my function to just the values in the first column of the inner arrays (1, 4, 7, 10, 13, 16, 19, 22, 25)
If I define my function as
def myfunc(x):
return x * x
and I want to get the following output (not a separate array containing just the sliced values)
[[[ 1 2 3]
[ 16 5 6]
[ 49 8 9]]
[[100 11 12]
[169 14 15]
[256 17 18]]
[[361 20 21]
[484 23 24]
[625 26 27]]]
I read a couple of tutorials, etc. that suggest slicing the array like array[:,:,0]
for getting the first column, but this approach does calculation on a new array and doesn't manipulate the elements in the original array. I could vstack the newly generated array with computed values with the original one, I guess. But is there a better solution?
Thank you very much in advance!