- Home /
Average of Vectors [Math]
I have a cube positioned according to the average of multiple vector points. The problem is that some times I'll have a point that is way too far and the cube will go off. Is there a possible way to make the values that are close to eachother have more influance than the extreme ones ?
Answer by Ymrasu · Feb 24, 2019 at 09:47 PM
There is something called Trimmed Mean to get the average while avoiding outliers. I don't think Unity has a built in function for it. Here is a quick one I threw together:
// using System.Linq;
List<Vector3> TrimmedMean(List<Vector3> list, float percent = 0.01f)
{
int trim = Mathf.CeilToInt(percent * list.Count);
List<Vector3> trimmedList = list.OrderBy(x => x.sqrMagnitude).ToList();
trimmedList.RemoveRange(trimmedList.Count-trim, trim);
trimmedList.RemoveRange(0, trim);
return trimmedList;
}
It takes a list of vectors and a percent (0-1). It sorts the vectors by their sqrMagnitude and trims out the top and bottom by the percentage of vectors in the list. That gets rid of extreme values, but can also get rid of normal values at the ends too.
Note that you may get better performance with something like this:
List<Vector3> trimmedList = list.OrderBy(x => x.sqr$$anonymous$$agnitude)
.Skip(trim).Take(list.Count - trim*2).ToList();
return trimmedList;
Since we just had a slightly similar question I just thought about this again and just realised that this might not doesn't work as intended. This solution sorts the position based on the absolute distance from the world origin. However that means any two vectors that are on the same radial orbit around the world center will be considered "near" to each other, even they could be very far apart. The current approach only works if the desired position is actually the world origin. Any other position will do a strange selection of vectors. This approach would remove jitter in the radial direction from the world origin, however orthogonal / tangential would jitter would stay. The "selection shape" would be a very strange morphed area.
We could run this for each axis; so if two vectors with the same magnitude but say an x of 23 and an x of -23, the outlier would be more likely to be trimmed.
Yes, this would improve the algorithm. Though you would always remove at least 4 values. So this only works if you always have enough values, say at least 7 or more, otherwise it just picks a rather random point of the given points and not an average.
Here's an illustration what your original code does:
The two circle mark the "distance" of the greatest and smallest points which are not trimmed, so the first and last element of the list after trim$$anonymous$$g. Any points within that torus would be considered "close". The yellow point is the calculated average from the trimmed list.
Your answer
Follow this Question
Related Questions
how do i keep a gameobject in between the union of 2 or more than 2 circles 0 Answers
How to get a vector3 (postion) 1 unit away from another in the direction of a 3rd vector3? 2 Answers
How to get translation from matrix? 1 Answer
Please help, bullet drop is wrong at certain angles ? 0 Answers
Given a vector, how do i generate a random perpendicular vector? 5 Answers