gernerating standard normal variable in matlab
7 views (last 30 days)
Show older comments
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
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
0 Comments
Accepted Answer
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])
What you probably meant to have is,
z= randn(n);
z=z-mean(z);
a=z*z'/n;
4 Comments
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)
a2=z*z'/n %fairly close to eye(2)
More Answers (1)
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
See Also
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!