## sum of rand matrix equal to zero

on 5 May 2015
Latest activity Commented on by Offroad Jeep

on 17 May 2016

### Hooman Habibi (view profile)

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 (view profile)

on 5 May 2015

Subtract the mean from the samples:
n=10; z=randn(n,n); z=z-sum(sum(z))/(n*n); sum(sum(z))

on 17 May 2016

on 5 May 2015
Edited by Bus141

### Bus141 (view profile)

on 5 May 2015

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 Comment

on 17 May 2016

on 5 May 2015

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.