Hi, i have this error when running my regression programme: Error using - Matrix dimensions must agree. Error in mpg_regression (line 55) X21 = X11-mean(X11); My arrays are all 1x82 (i checked using disp(size())). Any help would be appreciated.

1 次查看(过去 30 天)
mpg = importdata('carmpgdat.txt', '\t', 1);
VOL = mpg.data(:, 1);
HP = mpg.data(:, 2);
MPG = mpg.data(:, 3);
SP = mpg.data(:, 4);
WT = mpg.data(:, 5);
GPM = 1./MPG;
disp(size(VOL));
disp(size(HP));
disp(size(SP));
disp(size(WT));
X11 = [VOL HP SP WT];
mdl = fitlm(X11,MPG);
r2_1 = mdl.squared.Ordinary;
r2adj_1 = mdl.Rsquared.Adjusted;
p1 = 4;
X21 = X11-mean(X11);
X31 = X21/std(X21);
KS_1 = kstest2(X31, MPG);
  1 个评论
Greg
Greg 2016-12-24
Except the only array in question is absolutely NOT 1x82. X11 = [VOL HP SP WT]; makes X11 either 1x328 or 4x82 (and as I look at your other code, I'm pretty sure it's actually 82x1 making X11 82x4). You are likely trying to subtract a 1x4 from an 82x4. In R2016b with implicit expansion, this should result in a successful subtraction. Prior to R2016b, this won't work.

请先登录,再进行评论。

采纳的回答

Greg
Greg 2016-12-24
编辑:Greg 2016-12-24
Assuming the clarifications in my comment on the question are confirmed, there are a few quick answers.
1) Run your code in R2016b (or future release).
2) Use repmat on mean(X11) to expand it to 82x4.
3) Use bsxfun. The first example in "help bsxfun" is exactly what you're trying to do.
Good luck.
  2 个评论
Greg
Greg 2016-12-28
编辑:Greg 2016-12-28
As far as I know, bsxfun hasn't changed moving to R2016b. The change is that in many situations you simply don't need to call it anymore. Further, the automatic implicit expansion has better performance than calling bsxfun (yay faster code!).
Quote from R2016b Release Notes: "Previously, this functionality was available via the bsxfun function. It is now recommended that you replace most uses of bsxfun with direct calls to the functions and operators that support implicit expansion. Compared to using bsxfun, implicit expansion offers faster speed of execution, better memory usage, and improved readability of code."
https://www.mathworks.com/help/matlab/release-notes.html?rntext=&startrelease=R2014a&endrelease=R2016b&category=Mathematics&groupby=release&sortby=descending&searchHighlight=

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by