How to resolve Matrix dimensions error?

조회 수: 1 (최근 30일)
gp
gp 2020년 12월 10일
댓글: gp 2020년 12월 12일
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;

채택된 답변

Paul Hoffrichter
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
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?
gp
gp 2020년 12월 12일
I have no idea why it was deleted. I just noticed that issues. I will now post a new question and hope you can help me on this. Thank you

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

AVB
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.
  1. 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
  2. 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
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;
gp
gp 2020년 12월 10일
Yup, I did changed and transpose it also

댓글을 달려면 로그인하십시오.


Paul Hoffrichter
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
gp
gp 2020년 12월 10일
Is it used to get the same dimension size?
Paul Hoffrichter
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

댓글을 달려면 로그인하십시오.


Paul Hoffrichter
Paul Hoffrichter 2020년 12월 10일
y = polyval(I,x); % Error
Did you mean
y = polyval(x,I);

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by