Why do you think all aren't being calculated? Here is a modified version of your code, not changing the mechanics, which shows what you are getting.
a = 1:3;
b = 1:3;
for i = 1:3;
for j = 1:3;
a_pred = a(:,i);
b_pred = b(:,j);
disp([a_pred b_pred])
end
end
.
.
EDIT
.
Here is how to store all the results in chi2:
x = 1:3;
y = 1:3;
a = 1:3;
b = 1:3;
chi2 = zeros(9,3);
cnt = 0;
for i = 1:3;
for j = 1:3;
cnt = cnt + 1;
a_pred = a(:,i);
b_pred = b(:,j);
y_pred = a_pred + b_pred.* x
chi2(cnt,:) = (y-y_pred).^2;
end
end
chi2
And here is a vectorized version, using NPERMUTEK: IDX = npermutek(1:3,2);
y_pred = bsxfun(@plus,a(IDX(:,1)), bsxfun(@times,b(IDX(:,2)),x.'));
chi2 = bsxfun(@minus,y,y_pred.').^2
Not that it should be faster here...
Best Answer