Error using varfun function. "Matrix dimensions must agree."

조회 수: 1 (최근 30일)
Hello guys,
I am trying to do the following operation:
I want to multiply a table of 52824x9, named X, by the first column of a table of 9x4. The table's name is coef, the first column variable name is Estimate. Therefore I would like to do something like this:
yPred=X*coef.Estimate
I am using the varfun function in the following way:
yPred=varfun(@(x) x.*coef.Estimate,X);
However, I am getting the following message:
Error using tabular/varfun>dfltErrHandler (line 433)
Applying the function '@(x)x.*coef.Estimate' to the variable 'VarName3' generated the following error:
Matrix dimensions must agree.
As I understand, I am multiplying 52824x9*9x1, so I don't know where my mistake is.
If anyone knows how to solve this, or could provide any solution it would be very much appreciated.
Many thanks,
Natalia
  댓글 수: 2
the cyclist
the cyclist 2020년 3월 19일
Can you upload the data (or a small representative sample that exhibits the same error), in a MAT file, using the paper clip icon?
NATALIA ARREGUI GONZALEZ
NATALIA ARREGUI GONZALEZ 2020년 3월 19일
Hello,
I just uploaded it.
Thanks

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2020년 3월 19일
Several points:
  • "As I understand, I am multiplying 52824x9*9x1" No, you're using .*, the memberwise multiplication, not * the matrix multiplication. With .* the matrices must have the same size in all dimensions unless or one of them must be scalar in the mismatched dimensions.
  • "I want to multiply a table of 52824x9". Multiplication on tables are not defined. You could transform a table as long as all the variables are numeric matrices and multiply that but you cannot multiply tables. Typically tables contain heterogeneous data so multiplication wouldn't make sense.
  • varfun applies the given function to each variable of the table in turn. So the x that your function receives will be 52824x1. I don't think you want to use varfun.
  • The variables of your tables are day, month, period, holiday (which is just a 0/1 flag), etc. Does it even make sense to multiply variables like day or month, by your coeff variables?
  • It looks like you're trying to do a linear fit. Can you even do a linear fit when one of your variable is a 0/1 flag?
  댓글 수: 3
Guillaume
Guillaume 2020년 3월 19일
data{:,:} * coeff{:,:}
or
table2array(data) * table2array(coeff)
would multiply the content of both tables.
NATALIA ARREGUI GONZALEZ
NATALIA ARREGUI GONZALEZ 2020년 3월 20일
Many thanks, this is what I was looking for.
Best,
Natalia

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by