Is it possible to sum elements in an numpy array along an angle and not an axis.
I'm working with 2d arrays and it is only possible to sum along axis=0 or axis=1.
What i want is to sum along e.g. an degree of 45 or 60.
Example:
Matrix: [[1, 2], [3, 4]] and Angle: 45 degree.
The result should be something like [3, 1+4, 2] = [3, 5, 2] (sum from top left to bottom right).
Anyone an idea?
1 Answer
Its easy for what you call "45 degrees": numpy trace
import numpy as np
a = np.array([[1,2],[3,4]])
np.trace(a)
5
np.trace(a, offset=1)
2
np.trace(a, offset=-1)
3
and as a list:
>>> [np.trace(a,offset=i) for i in range(-np.shape(a)[0]+1, np.shape(a)[1])]
[3, 5, 2]
3 Comments
tnknepp
I did not know about trace, thanks. I was using np.diagonal in a similar fashion. However, the real question remains: how does the OP define non-45 degree diagonals.
Jonas
I could also give an answer for 90 and 0 degree diagonals ;)
tnknepp
Ha, I like your style. Plus one for wit!
skimage.transform.rotateto align the summation direction to the vertical or horizontal and then sum, although there would be changes in the values due to interpolation...np.cumsum()to add the points (point values) along the extraction line.60deg diagonal.