Discussion:
Matrix dimensions Mismatch
jin law
2018-10-13 03:34:58 UTC
Permalink
Greeting! I have 2set of 3X10 data,

Data=[866,1727,1777;
1588,2417,1828;
2515,4324,2097;
3117,4396,3029;
3666,4683,3719;
4354,4712,3767;
6213,5810,5581;
7481,6759,8070;
8722,7092,9126;
9602,9948,9782;]

data=[ 1723,2048,1341;
2578,3088,2359;
3781,3892,4946;
4469,4291,5629;
5837,5368,6142;
6565,6817,6600;
7976,7005,8050;
8017,8078,8182;
8393,9453,8337;
9648,9478,9146;]
for j=1:3;
y=Data(:,1)
X=[ones(10,1),data(:,j)]
end
y =

866
1588
2515
3117
3666
4354
6213
7481
8722
9602

X =

1 1723
1 2578
1 3781
1 4469
1 5837
1 6565
1 7976
1 8017
1 8393
1 9648

y =

866
1588
2515
3117
3666
4354
6213
7481
8722
9602

X =

1 2048
1 3088
1 3892
1 4291
1 5368
1 6817
1 7005
1 8078
1 9453
1 9478

y =

866
1588
2515
3117
3666
4354
6213
7481
8722
9602

X =

1 1341
1 2359
1 4946
1 5629
1 6142
1 6600
1 8050
1 8182
1 8337
1 9146
for j=1:3;
y=Data(:,1);
X=[ones(10,1),data(:,j)];
theta(:,j)=round(pinv(X'*X)*X'*y);
disp(theta)
end
-1634
1
-1634 -1941
1 1
-1634 -1941 -1737
1 1 1

to sub-press 2D to 1D I put another code;
Theta=theta(1,1:j);I tried theta(1,1:j)
for j=1:3;
y=Data(:,j);
X=[ones(10,1),data(:,j)];
theta(1,1:j)=round(pinv(X'*X)*X'*y);
end
error: A(I,J,...) = X: dimensions mismatch
for j=1:3;
y=Data(:,2);
X=[ones(10,1),data(:,j)];
theta(:,j)=round(pinv(X'*X)*X'*y);
Theta=theta(1,1:j);
end
Theta
Theta =

292 196 79

instead of doing it 3 times of y; I used nested loop.
for j=1:3;
y=Data(:,j);
for j=1:3;
X=[ones(10,1),data(:,j)];
theta(:,j,j)=round(pinv(X'*X)*X'*y);
Theta=theta(1,1:j,1:j);
end
end
Theta
Theta =

ans(:,:,1) =

-1352 -1766 -1319

ans(:,:,2) =

0 -1766 0

ans(:,:,3) =

0 0 -1319

So, the results I got is not the same as y=Data(:,1) and y=Data(:,2)and so on.
My Question is How to sub-press 2D theta to 1D? and For the nested loop is there any mistake?

Thanks,
jin


Sent from Mail for Windows 10

Loading...