Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

rand matrix for FM

조회 수: 1 (최근 30일)
Offroad Jeep
Offroad Jeep 2015년 3월 31일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi all,I want to generate a random matrix so that the sum of all the elements is zero. I mean the random numbers are so selected that the total sum of the matrix goes to zero.Regards. Early reply will be highly appreciated.
for example
a = rand(3)
a =
0.3000 0.5000 0.8000
-0.1000 -0.4000 -0.6000
-0.4000 0.8000 -0.9000
>> sum(sum(a))
ans =
0
  댓글 수: 2
Stephen23
Stephen23 2015년 3월 31일
Integer values only, or with decimal fraction?
Offroad Jeep
Offroad Jeep 2015년 3월 31일
Both for integer and decimals.............

답변 (3개)

Roger Stafford
Roger Stafford 2015년 3월 31일
편집: James Tursa 2015년 4월 1일
If your x values are subject to common upper and lower bounds, you can use my 'randfixedsum' function in the File Exchange, located at:
It is designed to give a uniform distribution on the hyperplane of values satisfying the condition of a predetermined sum - in your case a sum of zero.
  댓글 수: 1
John D'Errico
John D'Errico 2015년 3월 31일
And of course, this is the best answer.

Zoltán Csáti
Zoltán Csáti 2015년 3월 31일
I recommend you to generate the matrix of the required size and then modify one element of it so that the sum holds. E.g.
A = rand(3);
totalSum = sum(sum(A));
A(end,end) = A(end,end) - totalSum;
Then the sum will give you zero, aside from the round-off error.
  댓글 수: 2
John D'Errico
John D'Errico 2015년 3월 31일
편집: John D'Errico 2015년 3월 31일
It depends on how you want the elements themselves to be distributed. See that if they should originally be bounded in the interval [-1,1], then by the final shift, they often will no long be so bounded.
For example...
A = rand(3) *2 - 1;
A = A - sum(A(:))/numel(A)
A =
0.53571 -0.78404 0.51328
-0.81271 -0.68212 -0.24218
-0.32253 0.77053 1.0241
sum(A(:))
ans =
-2.2204e-16
See that while the sum is now zero, that now one of the elements actually exceeded 1, even though the original elements fell inside [-1,1].
Zoltán Csáti
Zoltán Csáti 2015년 4월 1일
Yes, you are right I didn't think of this aspect.

Brendan Hamm
Brendan Hamm 2015년 3월 31일
How about you create a random matrix and then subtract from each element the sum(matrix(:))/numel(matrix).
n = 4;
A = rand(4);
s = sum(A(:))/numel(A);
A = A - s;
sum(A(:))
  댓글 수: 5
Roger Stafford
Roger Stafford 2015년 4월 1일
The vagueness has to do with how you expect the set of vectors for which the sum has a given value, to be statistically distributed. Consider each vector as a point in n-dimensional vector space. What kind of statistical density function do you desire for the hyperplane therein where the vector sums are a given value? Is it to be uniform, gaussian, etc., and is it bounded?
Brendan Hamm
Brendan Hamm 2015년 4월 1일
They are currently U([-c c]) where c i s the sum of the originally sampled elements divided by the number of elements. You want them to be U([-1 1]), then just divide by the magnitude of the largest resulting element now:
A = A / max(abs(A(:)));

제품

Community Treasure Hunt

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

Start Hunting!

Translated by