gernerating standard normal variable in matlab

7 views (last 30 days)
I want to generate a vector 'z' of standard normal variable of size n. I want to create a matrix by multiplying z and z transpose, i.e. a=zz'. the matrix a should be ideally a unit matrix of size(n,n). I want to check wether the matrix is close to identity or not ?.
n = 10; % Specify the size of the vector and matrix
z= normrnd(1,0,[n,n]); % Generate a vector of standard normal variables
a = z * z'; % Compute the matrix by multiplying z and its transpose
threshold = 1e-6; % Define a threshold for closeness
norm_diff = norm(a - eye(n), 'fro'); % Compute the Frobenius norm of the difference
if norm_diff < threshold
disp('The matrix a is close to the identity matrix.');
else
disp('The matrix a is not close to the identity matrix.');
end
The matrix a is not close to the identity matrix.
at what value of n will i get the value close to identity?
The MATLAB command says that :
r = normrnd(mu,sigma,sz1,...,szN) generates an array of normal random numbers, where sz1,...,szN indicates the size of each dimension.
help normrnd
NORMRND Random arrays from the normal distribution. R = NORMRND(MU,SIGMA) returns an array of random numbers chosen from a normal distribution with mean MU and standard deviation SIGMA. The size of R is the common size of MU and SIGMA if both are arrays. If either parameter is a scalar, the size of R is the size of the other parameter. R = NORMRND(MU,SIGMA,M,N,...) or R = NORMRND(MU,SIGMA,[M,N,...]) returns an M-by-N-by-... array. See also NORMCDF, NORMFIT, NORMINV, NORMLIKE, NORMPDF, NORMSTAT, RANDOM, RANDN. Documentation for normrnd doc normrnd

Accepted Answer

Matt J
Matt J on 10 Jun 2023
Edited: Matt J on 10 Jun 2023
at what value of n will i get the value close to identity?
Well, with your current code, z will always contain only ones, so the answer is none.
n = 10; % Specify the size of the vector and matrix
z= normrnd(1,0,[n,n])
z = 10×10
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
What you probably meant to have is,
z= randn(n);
z=z-mean(z);
a=z*z'/n;
  4 Comments
Matt J
Matt J on 10 Jun 2023
Edited: Matt J on 10 Jun 2023
if I have a vector z=[z_1 z_2..........z_n] ' then theorectically zz^T should give an identity matrix.
No, that is false. If you generate millions of such vectors z and average together the z*z' matrices computed from them, then yes, you will converge to the identity matrix. However, a single such vector z will not be sufficient.
Here's another example that might make that clearer:
n=1e7;
z=normrnd(0,1,[2,n]);
a1=z(:,1)*z(:,1)' %not close at all to eye(2)
a1 = 2×2
0.0883 0.5635 0.5635 3.5951
a2=z*z'/n %fairly close to eye(2)
a2 = 2×2
0.9997 -0.0003 -0.0003 1.0003
rakesh kumar
rakesh kumar on 10 Jun 2023
very sorry, my theoretical assumptions was incorrect.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 10 Jun 2023
I'm a little confused. Why would you expect the result (of z*z') would be even remotely close to an identity matrix? In fact, your claim the result should be any kind of unit matrix would be wrong.
First of all, this is NOT even true:
z= normrnd(1,0,[n,n]); % Generate a vector of standard normal variables
z =
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
a =
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
that does NOT generate a vector of standard normal variates. It generates an array of size 10x10, with mean 1, and standard deviation ZERO. So when you do that, you get an array of all ONES. And therefore, the array a will be an array of all 10's.
But even if you DID generate an array of normal variates, z*z' would still NOT generate anything close to an identity matrix.
I think you are a little confused in this.
  3 Comments
rakesh kumar
rakesh kumar on 10 Jun 2023
Edited: Image Analyst on 10 Jun 2023
My question is, if z_i is a standard normal variable (i,e. with mean zero and variance 1) and if I have a vector z=[z_1 z_2..........z_n] ' then theoretically zz^T should give an identity matrix. I just want to generate that z in MATLAB.
The MATLAB command says:
r = normrnd(mu,sigma,sz1,...,szN) generates an array of normal random numbers, where sz1,...,szN indicates the size of each dimension.
help normrnd
NORMRND Random arrays from the normal distribution. R = NORMRND(MU,SIGMA) returns an array of random numbers chosen from a normal distribution with mean MU and standard deviation SIGMA. The size of R is the common size of MU and SIGMA if both are arrays. If either parameter is a scalar, the size of R is the size of the other parameter. R = NORMRND(MU,SIGMA,M,N,...) or R = NORMRND(MU,SIGMA,[M,N,...]) returns an M-by-N-by-... array. See also NORMCDF, NORMFIT, NORMINV, NORMLIKE, NORMPDF, NORMSTAT, RANDOM, RANDN. Documentation for normrnd doc normrnd
rakesh kumar
rakesh kumar on 10 Jun 2023
very sorry, my theoretical assumptions was incorrect.

Sign in to comment.

Categories

Find more on Interpolation 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!