Essentially, corr
is expecting column vectors as inputs, corr(X,Y) returns a p1-by-p2 matrix containing the pairwise correlation coefficient between each pair of columns in the n-by-p1 and n-by-p2 matrices X and Y.
So, you were asking it to calculate the correlation on 1000 columns with only 1 observation per column! This is why you received a NaN
.
NAN for correlation of random series in Matlab
-
02-09-2022 - |
Question
I wanted to generate 2 uncorrelated signals by using Matlab but I am having strange results, probably due to my limited experience.
I know that there are functions rand
and randn
. If I generate these 2 by using randn
the correlation of the 2 is always calculated and it is very low (as I was expecting).
z1 = randn(1,1000);
z2 = randn(1,1000);
corr(z1,z2) % it returns a very low number as expected
If I generate the 2 (or both) by using rand
like below the correlation is NAN
.
z1 = rand(1,1000);
z2 = rand(1,1000);
corr(z1,z2) % it returns a matrix (instead of vector?!) of NAN
If I use again the 2 (or both) using rand
but this time generating a matrix instead of a vector it works and the correlation matrix is fine.
z1 = rand(1000);
z2 = rand(1000);
corr(z1,z2) % it returns a matrix of finite values
Do you know why in the second case (the one with NAN) I am returned
- a matrix instead of a vector
- why these are all NAN instead of a single finite value?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow