sum of all anges is zero

3 views (last 30 days)
Offroad Jeep
Offroad Jeep on 10 Apr 2015
Commented: John D'Errico on 11 Apr 2015
Hi all , Hope all is fine,
I want to generate a n X n matrix of angle but the condition is that sum of all the angles is equal to zero.. my idea is given below but the sum is not zero how i can apply a condition........ Thanks in advance. I have taken 10 by 10 matrix of random angles.........
angle = (rand(10)-0.5)*360
angle =
-17.8050 -141.6050 -24.6910 127.0912 -29.7839 100.8907 -95.4792 16.9232 154.5789 51.9545
-149.8243 166.2833 147.8331 43.9398 -162.1244 -39.6940 -52.8629 -73.3245 99.2566 -43.7006
-97.5683 -178.3317 -114.5351 -53.6571 144.9778 -92.9911 115.6299 88.0894 -4.7550 112.1690
148.8015 98.9678 -85.0310 4.7698 160.1234 -34.5916 -174.4548 -111.9762 -23.0909 11.8172
-125.1439 114.2292 -127.6060 -35.3491 -3.2889 -145.2764 -164.5114 67.2392 -19.1579 -53.7382
117.2941 132.7301 -131.0153 -152.6520 -3.8691 -132.4896 -119.1636 -113.9360 -69.7142 158.0406
13.8033 -149.6031 132.9452 -93.6302 -58.4210 159.1382 53.6816 -47.3455 3.0631 135.3394
178.6085 -36.0782 28.6937 -135.6052 144.0194 164.2084 83.4201 45.2227 3.8778 18.0563
-151.8568 -86.4467 17.9497 -113.7932 -47.0712 27.0751 53.1885 100.8819 114.3460 44.0910
-20.6358 108.0247 -127.8163 -93.6171 -139.9670 -158.4794 -17.6675 -150.7947 106.1393 31.3361
>> sum(sum(angle))
ans =
-676.8780

Answers (2)

Image Analyst
Image Analyst on 10 Apr 2015

Star Strider
Star Strider on 10 Apr 2015
Subtract the mean rather than 0.5, since there is no guarantee that the mean will be exactly 0.5 with a less-than-infinite sample size:
rm = rand(10);
angle = (rm-mean(rm(:))) * 360;
angle_sum = sum(angle(:))
This produced:
angle_sum =
-4.1780e-012
which is acceptably close to zero (to me at least).
  1 Comment
John D'Errico
John D'Errico on 11 Apr 2015
Suppose the original numbers were sampled from the interval [-180,180] (in degrees.) The problem here is that the result need no longer satisfy that original set of bounds. This may or may not be acceptable. As well, the distribution of those numbers will not be sampled uniformly from the desired set.
Roger's randfixedsum solves all problems, sampling uniformly from the desired distribution.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!