Problem with "In an assignment A(:) = B, the number of elements in A and B must be the same"

1 回表示 (過去 30 日間)
Hi, I'm getting problem with the code that attached. When I run this code, it will show me this error: In an assignment A(:) = B, the number of elements in A and B must be the same

採用された回答

Jan
Jan 2017 年 3 月 25 日
編集済み: Jan 2017 年 3 月 25 日
Due to the missing definition of the global variables we cannot run your code. Perhaps. On the other hand, it looks like you define the global variables here only. Then why are they global?
You did not reveal the complete error message, such that we cannot know, where the error occurres. In consequence it is hard to guess, what's going on for the readers. But it is easy for you using the debugger. Type this in the command window:
dbstop if error
Now run your code. If it stops at the error, examine the values of the variables used in the failing line. The right hand side of the assignment will contain a number of elements, which differes from the number of elements on the left hand side. Because there are no useful comments in your code, a suggestion for improvements would be bold guessing only.
It is recommended, to write the intention of each block (or line!) of code into a comment, such that debugging is possible.
Note:
for i=1:5
cp = B+(2*C*y(6))+(3*D*(y(6))^2)+(4*E*(y(6))^3)+(5*H*(y(6))^4); % Btu/lb.R
end
This loop is meaningless, because cp is calculated 5 times with the same result: The loop contents does not depened on the loop counter i.
Overdoing with parenthesis decrease the readability of the code. Compare:
a1 = ((r1)*(-deltaH1)*(v));
a1 = -deltaH1 * r1 * v;
I prefer to move the unary minus to the front, but this is a question of taste.
If run time matters, consider that the power operation 10^-14 is very expensive, while reading a constant 1e-14 is almost free of costs during the execution:
H = [0.750735e-14, 0.053809e-14, 0.032141e-14, -1.387867e-14, 0.818450e-14];

その他の回答 (1 件)

Sepanta Gharib
Sepanta Gharib 2017 年 3 月 25 日
編集済み: Sepanta Gharib 2017 年 3 月 25 日
Thank you very much Jan, clearing global variables has solved this error. It's interesting! But having "NaN" Answers in my result, that goes to my engineering equations. No problem!

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by