Random vectors with null sum

조회 수: 2 (최근 30일)
Matteo Cacciola
Matteo Cacciola 2012년 6월 20일
Hello, I have a question for you. I have to initialize a number of 3D vectors, with fixed magnitude (intensity) equal to I, but the sum (in the geometrical sense) of vectors should be null. I prefer to work with angles (azimuth and elevation), but obviously a solution just considering the x-, y- and z-components of the vectors is fine, since thereafter I can make the transformation with cart2sph function.
Thanks a lot to all that could give me a support
Regards Matteo
  댓글 수: 2
Walter Roberson
Walter Roberson 2012년 6월 20일
Is the number of vectors to use known, or should it keep generating until it finds a solution?
Matteo Cacciola
Matteo Cacciola 2012년 6월 20일
Yes it is. Let me say that I have to generate N vectors, for instance N=100, each vectors has intensity I, for instance I=2, but the geometrical sum of vectors should be zero. Thanks

댓글을 달려면 로그인하십시오.

채택된 답변

Matteo Cacciola
Matteo Cacciola 2012년 6월 23일
I added a trivial solution. If the number to generate is even, I take couples of vectors. An element of each pair is randomly set, the other in opposite phase. If the number is odd, I do as for even vectors, but excluding three of them, which will be set manually as a null vector according to the rule of parallelogram. Thanks for your help.

추가 답변 (1개)

the cyclist
the cyclist 2012년 6월 20일
Even in just one dimension, it's a non-trivial problem to combine random numbers to have a fixed sum. (The question comes up with some frequency here.) The following doesn't do exactly what you want (I don't believe), but you might be able to bend it to your needs.
  댓글 수: 2
Matteo Cacciola
Matteo Cacciola 2012년 6월 20일
Thanks a lot, but unfortunately I already tried to exploit that code by considering that a vector is null if and only if all the components are null. So I tried to generate vectors where the sum of x- and y-components are null, then calculating each z-component by means of the well known relationship x^2 +y^2+z^2=I^2.
But I didn't take a suitable solution, since for some cases x^2+y^2>I^2, since random generation of x- and y-components are carried out independently. Any suggestion about?
Walter Roberson
Walter Roberson 2012년 6월 20일
Usually the easiest thing to do is to reject those (x,y) pairs and generate more. (Some of those might have problems too, so put yourself in a loop until you get N valid (x,y) pairs, each time generating (N minus number you have so far) samples.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by