Why I can't run the corrcoef for matrix

16 次查看(过去 30 天)
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 个评论
Karina Gutierrez
Karina Gutierrez 2016-11-28
编辑:Karina Gutierrez 2016-11-28
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

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2016-11-28
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 个评论
Karina Gutierrez
Karina Gutierrez 2016-11-28
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

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by