Error: Inner matrix dimensions must agree.

1 view (last 30 days)
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.

Accepted Answer

Jan
Jan on 20 Apr 2017
Edited: Jan on 20 Apr 2017
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.

More Answers (0)

Categories

Find more on Graphics Objects in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!