Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ay-mak · Feb 24, 2019 at 08:27 PM · vector3math

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 ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ay-mak · Feb 24, 2019 at 09:56 PM 0
Share

That's exactly what I was looking for thank you !

avatar image Bunny83 · Feb 25, 2019 at 03:52 AM 0
Share

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;
avatar image Bunny83 · Feb 26, 2019 at 01:26 PM 0
Share

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.

avatar image Ymrasu Bunny83 · Feb 26, 2019 at 02:34 PM 0
Share

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.

avatar image Bunny83 Ymrasu · Feb 26, 2019 at 04:46 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges