Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 3rd, 2005, 8:56 PM   #1
gpgemini
Newbie
 
Join Date: Jun 2005
Posts: 5
Rep Power: 0 gpgemini is on a distinguished road
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.
gpgemini is offline   Reply With Quote
Old Jun 3rd, 2005, 9:05 PM   #2
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
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.
EverLearning is offline   Reply With Quote
Old Jun 3rd, 2005, 9:09 PM   #3
gpgemini
Newbie
 
Join Date: Jun 2005
Posts: 5
Rep Power: 0 gpgemini is on a distinguished road
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.
gpgemini is offline   Reply With Quote
Old Jun 3rd, 2005, 9:19 PM   #4
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
Quote:
Originally Posted by gpgemini
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).
"taking regular mean" does not know that your values are angles and that 360 is a full circle: you'll have to subtract 360 or something like that.

Quote:
The code simply runs trough an array of degrees, nothing special.
post it if you want better help
EverLearning is offline   Reply With Quote
Old Jun 3rd, 2005, 9:29 PM   #5
gpgemini
Newbie
 
Join Date: Jun 2005
Posts: 5
Rep Power: 0 gpgemini is on a distinguished road
Quote:
Originally Posted by EverLearning
post it if you want better help
There is really nothing to post, I ran a regular mean:
[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.
gpgemini is offline   Reply With Quote
Old Jun 3rd, 2005, 10:16 PM   #6
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
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.
EverLearning is offline   Reply With Quote
Old Jun 4th, 2005, 1:41 AM   #7
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
is this some weird integer arithmetic problem?
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Jun 4th, 2005, 4:48 AM   #8
Nuticulus
Programmer
 
Nuticulus's Avatar
 
Join Date: May 2005
Location: England
Posts: 61
Rep Power: 4 Nuticulus is on a distinguished road
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;
}

Last edited by Nuticulus; Jun 4th, 2005 at 4:56 AM.
Nuticulus is offline   Reply With Quote
Old Jun 4th, 2005, 5:39 AM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5 grumpy is on a distinguished road
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)
grumpy is offline   Reply With Quote
Old Jun 4th, 2005, 5:42 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:43 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC