How to resolve Matrix dimensions error?

5 visualizaciones (últimos 30 días)
gp
gp el 10 de Dic. de 2020
Comentada: gp el 12 de Dic. de 2020
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;

Respuesta aceptada

Paul Hoffrichter
Paul Hoffrichter el 10 de Dic. de 2020
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 comentarios
Paul Hoffrichter
Paul Hoffrichter el 11 de Dic. de 2020
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 el 12 de Dic. de 2020
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

Iniciar sesión para comentar.

Más respuestas (3)

AVB
AVB el 10 de Dic. de 2020
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 comentarios
AVB
AVB el 10 de Dic. de 2020
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 el 10 de Dic. de 2020
Yup, I did changed and transpose it also

Iniciar sesión para comentar.


Paul Hoffrichter
Paul Hoffrichter el 10 de Dic. de 2020
E =(Po-y).^2; % ( 1x21 - 1x3 ) .^ 2
is this what you want:
E =(Po-y').^2; % ( 1x21 -3x1 ) .^ 2
  3 comentarios
gp
gp el 10 de Dic. de 2020
Is it used to get the same dimension size?
Paul Hoffrichter
Paul Hoffrichter el 10 de Dic. de 2020
Editada: Paul Hoffrichter el 10 de Dic. de 2020
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

Iniciar sesión para comentar.


Paul Hoffrichter
Paul Hoffrichter el 10 de Dic. de 2020
y = polyval(I,x); % Error
Did you mean
y = polyval(x,I);

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by