Effacer les filtres
Effacer les filtres

Why I can't run the corrcoef for matrix

15 vues (au cours des 30 derniers jours)
Karina Gutierrez
Karina Gutierrez le 28 Nov 2016
Hi All, I am trying to run corrcoef to obtain the matrix of correlation coefficients and p-values but keep getting an error. I am using:
R = corrcoef (tbl);
(tbl is a 1050x5 table)
And this is the error I get:
Undefined function 'sum' for input arguments of type 'table'.
Error in cov (line 154) xc = bsxfun(@minus,x,sum(x,1)/m); % Remove mean
Error in corrcoef>correl (line 206) r = cov(x);
Error in corrcoef (line 89) r = correl(x);
Could someone please advise me what am I doing wrong and how to run this analysis?
Thanks in advance, Karina
  1 commentaire
Karina Gutierrez
Karina Gutierrez le 28 Nov 2016
Modifié(e) : Karina Gutierrez le 28 Nov 2016
Never mind, I just give it a second thought and realized that I had the input with the format of a table and not a matrix and this was creating the issue.
I created a matrix with the same data as the table and now it run just fine.
I guess I still don't understand the difference between a matrix and a table?
Cheers, Karina

Connectez-vous pour commenter.

Réponse acceptée

Steven Lord
Steven Lord le 28 Nov 2016
You can't compute the correlation coefficient of a table since a table could contain non-numeric data. Instead, you can compute the correlation coefficient of one or more variables in the table.
x = (1:10).';
y = x.^2;
t = table(x, y);
ycc = corrcoef(t.x, t.y)
If your variables can be concatenated into a matrix, you can pass the Variables from the table into corrcoef.
cc = corrcoef(t.Variables)
  1 commentaire
Karina Gutierrez
Karina Gutierrez le 28 Nov 2016
Thanks for your super fast response Steve, I concatenated the variables into a matrix and was able to run the correlation coefficient.
So just for clarification, is the main difference between a table and a matrix that tables can store non numerical values and matrices are only for numerical values? And if so, when doing any calculations is better to have matrices right?
Sorry, I am pretty new to Matlab and I still get easily confused.
Cheers,
Karina

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by