# sum of rand matrix equal to zero

조회 수: 17(최근 30일)
댓글: Offroad Jeep 2016년 5월 17일
Dear All,
I want to generate matrix A ,i.e. n by n random matrix but the sum(sum(A)) = zero. Help in this regard will be highly appreciated........
Regards..........

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

### 채택된 답변

Hooman Habibi 2015년 5월 5일
Subtract the mean from the samples:
n=10; z=randn(n,n); z=z-sum(sum(z))/(n*n); sum(sum(z))

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

### 추가 답변(2개)

Bus141 2015년 5월 5일
편집: Bus141 2015년 5월 5일
The command randn pulls from a normal distribution with distribution N(0,1), implying with a large enough sample the mean should be zero. The sum of all dimensions should also converge toward zero since the values above and below the mean will cancel each other out, as they have equal probability.
##### 댓글 수: 1표시숨기기 없음

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

What kind of random distribution do you want? Here I use normal distribution, for other distributions just replace randn by the respective function:
n=100;
A=randn(100);
B=A-mean(A(:));
sum(B(:))
ans =
3.8913e-13
That's not exactly zero, but you'll not easily come close due to numerical precision.

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

### 범주

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

### Community Treasure Hunt

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

Start Hunting!