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

arrayskstestsregressionstats

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);

Best Answer

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.