Subscripted assignment dimension mismatch.(matrices)

1 回表示 (過去 30 日間)
cakey
cakey 2014 年 11 月 30 日
コメント済み: John D'Errico 2014 年 11 月 30 日
J = eye(4); % 4x4 identity matrix
u = sum(x);
N=[exp(cos(u))*sin(u),exp(cos(u))*sin(u),exp(cos(u))*sin(u),exp(cos(u))*sin(u);
2*exp(cos(2*u))*sin(2*u),2*exp(cos(2*u))*sin(2*u),2*exp(cos(2*u))*sin(2*u),2*exp(cos(2*u))*sin(2*u);
3*exp(cos(3*u))*sin(3*u),3*exp(cos(3*u))*sin(3*u),3*exp(cos(3*u))*sin(3*u),3*exp(cos(3*u))*sin(3*u);
4*exp(cos(4*u))*sin(4*u), 4*exp(cos(4*u))*sin(4*u),4*exp(cos(4*u))*sin(4*u),4*exp(cos(4*u))*sin(4*u)];
for i=1:4
for j=1:4
J(i,j) = J(i,j) + N
end
end
But I get that error message, how can I make this work?
  8 件のコメント
cakey
cakey 2014 年 11 月 30 日
編集済み: cakey 2014 年 11 月 30 日
Here it is. Now I keep getting an error for the matrix:
if true
% code
end
function J = JJ(x)
J = eye(4); % 4x4 identity matrix
x=[exp(cos(u));
exp(cos(2*u));
exp(cos(3*u));
exp(cos(4*u))]
u = sum(x);
N=[exp(cos(u))*sin(u),exp(cos(u))*sin(u),exp(cos(u))*sin(u),exp(cos(u))*sin(u);
2*exp(cos(2*u))*sin(2*u),2*exp(cos(2*u))*sin(2*u),2*exp(cos(2*u))*sin(2*u),2*exp(cos(2*u))*sin(2*u);
3*exp(cos(3*u))*sin(3*u),3*exp(cos(3*u))*sin(3*u),3*exp(cos(3*u))*sin(3*u),3*exp(cos(3*u))*sin(3*u);
4*exp(cos(4*u))*sin(4*u), 4*exp(cos(4*u))*sin(4*u),4*exp(cos(4*u))*sin(4*u),4*exp(cos(4*u))*sin(4*u)];
for i=1:4
for j=1:4
J(i,j) = J(i,j) + N
end
end
if true
% code
end
John D'Errico
John D'Errico 2014 年 11 月 30 日
Probably because in this latest code, you have written this as a function, where you pass in some variable x. But then you overwrite what you passed in as x, using a variable u, but you have not defined u until after x is created, as a function of u!
Perhaps you need to think about what you are trying to do.

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2014 年 11 月 30 日
It looks like N is a 4x4 matrix, and you are trying to assign it to just one position of J.
It is difficult to know what was intended here, instead of
for i=1:4
for j=1:4
J(i,j) = J(i,j) + N
end
end
maybe it should just be
J = J + N
That is just a pure guess, based on the size of the matrices.
  1 件のコメント
cakey
cakey 2014 年 11 月 30 日
I tried this, but still dimension mismatch. I'll try your way:
if true
% code
end
function J = JJ(x)
x = [2.5; 2.0; 1.4; 0.9]
f = @(x) x - exp(cos([1:4]*sum(x)));
J = eye(4); % 4x4 identity matrix
if true
% code
end
u = sum(x);
N=[exp(cos(u))*sin(u),exp(cos(u))*sin(u),exp(cos(u))*sin(u),exp(cos(u))*sin(u);
2*exp(cos(2*u))*sin(2*u),2*exp(cos(2*u))*sin(2*u),2*exp(cos(2*u))*sin(2*u),2*exp(cos(2*u))*sin(2*u);
3*exp(cos(3*u))*sin(3*u),3*exp(cos(3*u))*sin(3*u),3*exp(cos(3*u))*sin(3*u),3*exp(cos(3*u))*sin(3*u);
4*exp(cos(4*u))*sin(4*u), 4*exp(cos(4*u))*sin(4*u),4*exp(cos(4*u))*sin(4*u),4*exp(cos(4*u))*sin(4*u)];
for i=1:4
for j=1:4
J(i,j) = J(i,j) + N
end
end

サインインしてコメントする。

その他の回答 (1 件)

John BG
John BG 2014 年 11 月 30 日
Have you tried 1. allocate initial J just after defining N to make sure J and N have same size with: J=zeros(size(N))
2. Instead of 2 for loops, would it be ok for you to use J=J'+N

カテゴリ

Help Center および File ExchangeChemistry についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by