Issue
I am attempting to add together specific elements of two numpy arrays.
For example consider these two arrays:
f1 = np.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.]]
])
f2 = np.array([
[[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]],
[[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]],
[[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]
])
I would like to create a new array that looks like this:
[[0.5, 0.5, 0.5],
[0.5, 0.5, 0.5],
[0.5, 0.5, 0.5]]
The operation I would like to perform would look something like (f1[r][c][0]+f2[r][c][0])/2
for each array of three values in f1 and f2.
I know that np.add(f1,f2)/2
would result in something close to what I'm looking for, except that it would perform the add and scale operations on every element of the array, as opposed to just the first element in each length 3 subarray. Is the best way to do this just breaking up each original array into three separate arrays, thus allowing me to use np.add(f1,f2)/2
?
Apologies if this is a duplicate, I couldn't seem to find anything about performing an operation like this.
Solution
You can index the desired position before computing the sum:
(f1[...,0]+f2[...,0])/2
Output:
array([[0.5, 0.5, 0.5],
[0.5, 0.5, 0.5],
[0.5, 0.5, 0.5]])
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.