Borrar filtros
Borrar filtros

Correlation between 2 vectors give a matrix full of Nan

2 visualizaciones (últimos 30 días)
marcello
marcello el 30 de Oct. de 2023
Comentada: Voss el 31 de Oct. de 2023
Hi, im trying to calculate the correlation between the vector cost and cost2:
A=5;
f=1;
ph1=0;
cost=A*cos(pi*f*t+ph1);
cost2 = A*cos(2*(pi*f*t+ph1))+1;
I need to draw the graphic of the correlation but the result is a matrix full of Nan and the plot is blank graph.
I've already tried to use c = corr(cost,cost2,'rows','complete') but the result is still a matrix full of Nan values.

Respuesta aceptada

Voss
Voss el 30 de Oct. de 2023
From the documentation for corr:
rho = corr(X) returns a matrix of the pairwise linear correlation coefficient between each pair of columns in the input matrix X.
If t is a row vector (unknown since t was not given), then your vectors are row vectors, so each column is a single element. You don't want to be calculating correlations between one element and another element; you want the correlation between one entire vector and the other entire vector, so make them into column vectors so that corr can operate on them properly.
t = 0:0.01:1;
A=5;
f=1;
ph1=0;
cost=A*cos(pi*f*t+ph1);
cost2 = A*cos(2*(pi*f*t+ph1))+1;
c = corr(cost(:),cost2(:),'rows','complete')
c = 1.4630e-16
plot(cost)
hold on
plot(cost2)

Más respuestas (1)

Walter Roberson
Walter Roberson el 30 de Oct. de 2023
Editada: Walter Roberson el 30 de Oct. de 2023
t = linspace(0,1) .'; %COLUMN
A=5;
f=1;
ph1=0;
cost=A*cos(pi*f*t+ph1);
cost2 = A*cos(2*(pi*f*t+ph1))+1;
c = corr(cost,cost2,'rows','complete')
c = 1.1679e-16
Perhaps you are expecting a correlation for each t value.
When you pass in an N x M first parameter and an P x Q second parameter and request 'rows' then the number of rows must be equal -- so N x M and N x Q . The size of the output would then be M x Q -- number of columns of the first times number of columns of the second. Unless, that is, the data you pass in only has one row, in which case the outputs will be all NaN.
I suspect you passed in two row vectors, in which case you would get out size(cost,2) by size(cost2,2) of all NaN.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by