Matrix dimensions must agree error
Información
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Mostrar comentarios más antiguos
I have the following code and couldn't figure out what I am doing wrong. I am getting the following error,
X0 = [0.001;0.001;0.001;0.001;0.001;0.001];
maxIter =100;
tolX = .01;
% Computation
X = X0;
Xold = X0;
R = inv(j),
for i =1:maxIter
[f,j]=new(X);
X = (X-R*f);
err(:,i) = (X-Xold)
Xold = X;
X =[0 pi/2; 0 pi/2; 0 pi/2; 0 pi/2; 0 pi/2; 0 pi/2;];
if (err(:,i)<tolX)
break;
end
end
5 comentarios
Sindar
el 21 de En. de 2020
please copy the full error (all red text) into your answer
Avismit Dutta
el 21 de En. de 2020
Avismit Dutta
el 21 de En. de 2020
Avismit Dutta
el 21 de En. de 2020
Sindar
el 21 de En. de 2020
So, if I'm reading this correctly, X should be 6x1, f should be 6x1, and R should be 6x6? So R*f should be 6x1. Check these sizes
Respuestas (1)
Guillaume
el 21 de En. de 2020
The error message is very clear, R*f is not the same size as X (or a size compatible with X), hence you can't do the subtraction. Since we don't know what R or f is we can't tell what their size is.
Note that numbered or sequentially named variables are a bad idea. It forces you to complicate the code. For example, this whole lot:
a = X(1);
b = X(2);
c = X(3);
d = X(4);
e = X(5);
f = X(6);
fval(1,1)=cosd(5*a)+cosd(5*b)+cosd(5*c)+cosd(5*d)+cosd(5*e)+cosd(5*f);
fval(2,1)=cosd(7*a)+cosd(7*b)+cosd(7*c)+cosd(7*d)+cosd(7*e)+cosd(7*f);
fval(3,1)=cosd(11*a)+cosd(11*b)+cosd(11*c)+cosd(11*d)+cosd(11*e)+cosd(11*f);
fval(4,1)=cosd(13*a)+cosd(13*b)+cosd(13*c)+cosd(13*d)+cosd(13*e)+cosd(13*f);
fval(5,1)=cosd(17*a)+cosd(17*b)+cosd(17*c)+cosd(17*d)+cosd(17*e)+cosd(17*f);
fval(6,1)=cosd(19*a)+cosd(19*b)+cosd(19*c)+cosd(19*d)+cosd(19*e)+cosd(19*f);
can be replaced with just:
%X is a column vector
fval = sum(cosd([5; 7; 11; 13; 17; 19] .* X.'), 2);
4 comentarios
Avismit Dutta
el 21 de En. de 2020
Walter Roberson
el 21 de En. de 2020
Which MATLAB release are you using?
Avismit Dutta
el 21 de En. de 2020
Walter Roberson
el 21 de En. de 2020
fval = sum(bsxfun(@times, cosd([5; 7; 11; 13; 17; 19], X.')),2);
La pregunta está cerrada.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!