문제

I am writing the program in Cocoa but I think that the solution must be quite universal.

I have a set of points represented by 3D vectors. Each point has a weight assigned to it. The weight is in the range from 0 to 1. The sum of all weights isn't equal to 1.

How should the weighted mean point be calculated from such set?

Either programmatic or pure mathematical solution will be helpful. Of course if Cocoa has some specific tools for solving this task, I would very appreciate this information.

도움이 되었습니까?

해결책

Simply sum all vectors scaled by their weight. Finally, divide by the sum of all weights. This has the same effect as first normalizing all weights to sum to 1.

Pseudo-code:

sum = [0, 0, 0]
totalWeights = 0
for each point p with associated weight w:
    sum += p * w
    totalWeights += w
mean = sum / totalWeights
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top