Why I can't run the corrcoef for matrix

조회 수: 3 (최근 30일)
Karina Gutierrez
Karina Gutierrez 2016년 11월 28일
댓글: Karina Gutierrez 2016년 11월 28일
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개)

카테고리

Help CenterFile Exchange에서 Data Preprocessing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by