Error: Inner matrix dimensions must agree.

1 回表示 (過去 30 日間)
Mohammad Sohel Rana
Mohammad Sohel Rana 2017 年 4 月 20 日
編集済み: Jan 2017 年 4 月 20 日
Size of Fh and Reh is [15 25] and size of Prh is [1 25]
for i=1:1:N1
for j=1:1:length(Dd)
Nuh(:,i)=((Fh(:,i)/8)*(Reh(:,i)-1000)*Prh(i))/(1+12.7*(Fh(:,i)/8).^(0.5).*(Prh(i).^(2/3))-1);
end
end
How I can overcome the error.

採用された回答

Jan
Jan 2017 年 4 月 20 日
編集済み: Jan 2017 年 4 月 20 日
We have to guess what you want to achieve, because all we see is the failing code. If you mention the intention (preferrably in a comment, such that you can read it also, when you try to maintain the code in 2 years), suggesting an improvement is more reliable.
Look at this expression:
(Fh(:,i) / 8) * (Reh(:,i) - 1000) * Prh(i))
This is the multiplication of two [15 x 1] vectors, but this is not allowed (in Matlab and mathematically). What is the wanted output? Either use the elementwise multiplication:
(Fh(:,i) / 8) .* (Reh(:,i) - 1000) * Prh(i))
or transpose the 1st vector:
(Fh(:,i).' / 8) * (Reh(:,i) - 1000) * Prh(i))
The division by / will suffer from the same problem. Again either adjust the array dimensions or use the elementwise operation ./ .
You can solve such problems by your own: Split the complicated command into small blocks and process them one by one:
a = Fh(:,i) / 8;
b = (Reh(:,i) - 1000) * Prh(i);
c = sqrt(Fh(:,i) / 8); % SQRT is much faster than ^0.5
... etc
d = a * b;
Nuh(:,i) = (d / (1+12.7*c.*(Prh(i).^(2/3))-1);
Now you can check the dimension of the smaller blocks much easier and find problems efficiently.
Note: Inserting spaces around operators increase the readability.

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by