How to resolve Matrix dimensions error?
2 次查看(过去 30 天)
显示 更早的评论
Hi, May I know how to resolve this error? Thank You!
Error part Matrix dimensions must agree.
Error in MathCW (line 15) v = [ones(21,1) I2 I2.^2].\PN0;
0 个评论
采纳的回答
Paul Hoffrichter
2020-12-10
I made the code more readable to me, and adjusted the dimensions which are in the annotations.
% Increment of current
I=0:0.5:(0.5*20); % 1x21
I2=transpose(I); % Power measurements 21x1
Po=I.*I*100; % 1x21
for experiment = 1:10 % Noise with sd 0
noise0=randn(1,21); % 1x21
PN0=Po+(0.*noise0); % 1x21
scatter(I,Po); hold on
scatter(I,PN0,'filled');hold on;
hold off
x = polyfit(I,PN0,2); % 1x3
A = [ones(21,1) I2 I2.^2]; % 21x3
PN0tr = PN0'; % 21x1
xx = A .\ PN0tr; % 21x3 .\ 21x1
% I has a 0 in it, so you are dividing by 0 - not good
% A .\ PN0tr is the matrix with elements: PN0tr(i,j) / A(i,j)
y = polyval(x,I); % 1x21
errDiff = Po-y; % ( 1x21 - 1x21 ) .^ 2
E =(errDiff).^2;
Em=mean(E);
disp( ['Em = ' num2str(Em) ])
end
Now the output is:
Em = 6.0104e-25
13 个评论
Paul Hoffrichter
2020-12-11
I and other experts were starting to help you in your other question. Can you tell me why it was deleted, or should I contact Mathworks directly?
更多回答(3 个)
AVB
2020-12-10
Next time please make sure you copy the code block in your question using the 'Insert a line of code' option.
There were two issues.
- The matrix [ones(21,1) I2 I2.^2] is of 21x3 size hence PNO should have compatible size which means it should be of size 21x1
- The first argument in the polyval function should be polynomial coefficients (in descending powers) of an nth-degree polynomial. See polyval
Below is your updated code:
% Increment of current
I=0:0.5:(0.5*20);
I2=transpose(I);
% Power measurements
Po=I.*I*100;
for experiment = 1:10
% Noise with sd 0
noise0=randn(1,21);
PN0=Po+(0.*noise0);
scatter(I,Po);
hold on
scatter(I,PN0,'filled');
hold off
x = polyfit(I,PN0,2);
v = [ones(21,1) I2 I2.^2].\PN0';
y = polyval(x,I);
% Error
E =(Po-y).^2;
Em=mean(E);
end
4 个评论
AVB
2020-12-10
did you change the polyval function arguments? should be ......
y = polyval(x,I);
If you want to keep your y as is as below,
y = polyval(I,x);
then do transpose on y while computing E
E =(Po-y').^2;
Paul Hoffrichter
2020-12-10
E =(Po-y).^2; % ( 1x21 - 1x3 ) .^ 2
is this what you want:
E =(Po-y').^2; % ( 1x21 -3x1 ) .^ 2
3 个评论
Paul Hoffrichter
2020-12-10
编辑:Paul Hoffrichter
2020-12-10
Yes. However, I have already fixed a core problem, so now the dimensions are the same. But the above dimensions are interesting. I am pretty sure this would not have worked 10 years ago. Here is a simple example to illustrate some newer matrix/vector operations.
>> t % 1x4
t =
10 20 30 40
>> u % 7x1
u =
0
2
4
6
8
9
10
>> t - u
ans =
10 20 30 40
8 18 28 38
6 16 26 36
4 14 24 34
2 12 22 32
1 11 21 31
0 10 20 30
If u was also a row vector, then its dimensions would have to equal t's dimensions. For example:
s = % 4x1
5 5 5 5
>> t - s
ans =
5 15 25 35
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!