Matrix dimensions must agree error
정보
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
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
Sindar
2020년 1월 21일
please copy the full error (all red text) into your answer
Avismit Dutta
2020년 1월 21일
Avismit Dutta
2020년 1월 21일
Avismit Dutta
2020년 1월 21일
Sindar
2020년 1월 21일
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
답변 (1개)
Guillaume
2020년 1월 21일
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
Avismit Dutta
2020년 1월 21일
Walter Roberson
2020년 1월 21일
Which MATLAB release are you using?
Avismit Dutta
2020년 1월 21일
Walter Roberson
2020년 1월 21일
fval = sum(bsxfun(@times, cosd([5; 7; 11; 13; 17; 19], X.')),2);
이 질문은 마감되었습니다.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!