# sum of rand matrix equal to zero

7 views (last 30 days)

Show older comments

Offroad Jeep
on 5 May 2015

Commented: Offroad Jeep
on 17 May 2016

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..........

##### 0 Comments

### Accepted Answer

Hooman Habibi
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))

### More Answers (2)

Bus141
on 5 May 2015

Edited: Bus141
on 5 May 2015

Michael Haderlein
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.

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!