Averaging angle

I reviewed some solutions here, but none of them provide what I need, therefore:

I need to average the array of angles (from 0 to 359.9, without negatives) (A1 + A2 + A3 + An) / n

The problem is when you get the array {1, 359, 2, 358} if you use a formula above 180, but in fact it should be 0.

Any thoughts?

+4
Mar 17 '11 at 18:36
source share
4 answers

Add the unit vectors of each corner and convert the resulting vector back to a corner. If the result vector is zero length, the inputs cancel each other and the result is undefined.

The unit vector has a length of 1, and its lengths x and y are given by the cosine and sine of the angle. Thus, you average your examples, as in the following pseudocode:

x = cos(radians(1)) + cos(radians(359)) + cos(radians(2)) + cos(radians(358)); y = sin(radians(1)) + sin(radians(359)) + sin(radians(2)) + sin(radians(358)); angle = degrees(atan2(y, x)); 
+16
Mar 17 '11 at 18:39
source share

A ray emerging from a circle can be represented in more than one way. It can be 0 degrees, 360 degrees, 720 degrees, etc. You need to determine what the right acceptable answer is for you, and secret intermediate answers to the final answer before submitting it.

 1 + 359 + 2 + 358 = 360 + 360 = 720 degrees in total 720 / 4 = 360 / 2 = 180 degrees on average 

It is not that the answer should be zero, it is that 359 degrees is not equivalent to -1 degrees, because the angle goes in a circle "differently."

0
Mar 17 2018-11-18T00:
source share

Do you want to round it, that is, 0 should come after 359 degrees?

take the result module from 360.

 avg = ( (a1 + a2 + an) /n ) % 360 
0
Mar 17 '11 at 18:43
source share

The degree of averaging is a little more complicated than it seems. I had to do this at work the other day and I want to share my findings.

  • Adding 360 to any of your values ​​should not change on average.
    e.g. avg (2 4 18) = avg (362,4,18) = 8
  • A shift of all values ​​in a constant value should shift the average value equally.
    e.g. avg (2-3,4-3,18-3) = avg (359,1,15) = 8-3
  • If your degrees are "undo", for example, 0.120,240, the average value is undefined
    (i.e. there are several equally good answers).
    I see no way to handle this except for the error message.

After falling into several traps, I got the following definition: The average value that minimizes the variance.

After setting the average value, the variance can be calculated using:

  • Shift all values ​​by the same angle, so the average is 180.
    (This is equivalent to "cutting" a circle on the opposite side of the middle.)
  • Normalization of all angles between 0 and 360.
  • Calculation of the normal average.
  • Shift the average backward by the angle by which you shifted the values ​​to (step 1).

It takes O time (n ^ 2).

However, if all your values ​​are within the range of 180 degrees, you can shift the values, such that 0 is in the largest gap of values, and then calculate the normal average value. This takes O (n) time.

0
Oct 24 '14 at 21:12
source share



All Articles