![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 5
Rep Power: 0
![]() |
calculation angle mean
Hi, I am trying to calculate the mean of a set of angles, but I can't get pass the problem the if I have lots of angles at 0.01 degrees and lots of angles at 359.99 degrees the mean is found to be around 180 and not around zero as one would expect.
I've tried approaching this from different angles but I keep hitting the same problem, the fact the numbers are not continuous. Can you please help me ? Thank you, Guy. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
You might have to modify the input file so that your input set of angles reflects the fact that 359.99 is almost the same as 0. Maybe using unsigned type if you need an absolute value for the mean and subtract from every angle 360 degrees
Post some relevant code to get a better idea about design of your program. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Posts: 5
Rep Power: 0
![]() |
That's not the problem, the problem is algorithmic and occurs even if I take lots of angles at 1 degree and lots of angles at 359 degrees, a regular mean calculation will give as a results the value "180", which is not the correct answer (should be +-0).
The code simply runs trough an array of degrees, nothing special. |
|
|
|
|
|
#4 | ||
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
Quote:
Quote:
![]() |
||
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Jun 2005
Posts: 5
Rep Power: 0
![]() |
Quote:
[pseudo code] loop sum += val*weight; Twieght += weight; return sum / TWeight; And after viewing the results I realized that is no good for me. Pls don't tell me to sub 360 or something - I wouldn't have come to ask for help had I not needed it - in other words I went trough all the sub 360 or something options, they don't solve the problem. |
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
Ok, something like this, although i do not know how you would want to calculate mean for angles. My assumption: -180 <= angle <=180:
#include <stdio.h>
#include <math.h>
int main()
{
double array[9] = {100, -250, 90, 20, 0.1, 359, 359, 0.1, 0.1};
double mean = 0;
double sum = 0;
int i = 0;
//adjust input so that |negative_angle| > 180 is positive
for (i = 0; i < 9; i++)
{
if((array[i] < 0) && (abs(array[i]) > 180))
{
array[i] = array[i] + 360;
}
}
//adjust input so that any_angle > 180 is negative
for(i = 0; i < 9; i++)
{
if(array[i] > 180)
{
array[i] = array[i] - 360;
}
}
//display new array of angles
printf("*****************\n");
for(i = 0; i < 9; i++)
{
printf("%d\n", array[i]);
}
//get mean
for (i = 0; i < 9; i++)
{
sum += array[i];
}
mean = sum/(i + 1);
printf("%d\n", mean);
return 0;
}EDIT: i know it's not a good programming practice to push everything in main() but you get the idea... Last edited by EverLearning; Jun 3rd, 2005 at 10:27 PM. |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
is this some weird integer arithmetic problem?
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#8 |
|
Programmer
Join Date: May 2005
Location: England
Posts: 61
Rep Power: 4
![]() |
Firstly, you need to work out a set system. Will you be using only unsigned floats (0 to 360), or will angles be represented as signed floats (-180 to +180)? You have to decide. I'm guessing that the unsigned method is what you want.
I think what you need to do here is, if you want to input unsigned floats, then you can convert these to signed floats, and then averaging them will be easy. Then convert the output to unsigned floats again. Here's some (not fully tested) code to do it: #include <stdio.h>
int main()
{
float angles[2] = {1.0f, 1.0f}, s=0;
int i;
printf("The average of angles: ");
for(i=0;i<sizeof(angles)/sizeof(float);++i)
{
printf("%f ", angles[i]);
if(angles[i]>180)
angles[i] -= 360.0f;
s += angles[i];
}
s /= sizeof(angles)/sizeof(float);
if(s<0)
s += 360.0f;
printf("is %f\n", s);
return 0;
}
__________________
http://www.nuticulus.net/hackmysig/sig.PNG Give my site a chance, you know you want to ;-) Google will solve 99% of your problems. Including those with your sex life. Caution, may <strike>contain</strike> be nuts. Last edited by Nuticulus; Jun 4th, 2005 at 4:56 AM. |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5
![]() |
The mean of 359.99 and 0.01 is 180.0 last I checked.
Assuming your individual angles are in the range [0, 360), what you need to do is something like this; 1) Compute the mean 2) Convert the angles so they are in the range [-180, 180). Compute the mean of those and add 180. 3) If the two values are the same (within machine precision), this is your mean. 4) If the values are different, convert the angles so they are in the range [90, 450). Compute the mean and subtract 90. As a sanity check, this value should be the same (within machine precision) as one of the previous two. This is your desired result. I'll leave it as an exercise for you to explain why this will work (and why the four steps are needed) ![]() |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The fact is that if you take a bunch of scalar numbers between 0 and 360, the mean is going to be between 0 and 360, converging on 180. If your numbers are angles, points on a circle, then their position is relative to some point on the circle which you have to choose. Most people choose 0 and scale all the numbers relative to that value. That's the point you're forgetting. No matter how frustrated you are, telling people NOT to make responses because YOU, in your frustration, have tried everything, is not the way to wind up with a good answer. To phrase it another way: you say you came because you need help -- don't snap at the helping hand, just pay attention and see if you can learn something.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|