What does this mean? "In an assignment A(I) = B, the number of elements in B and I must be the same"

1 回表示 (過去 30 日間)
p0=1.207;
t=0;
9=9.81;
i=0
for i=1:1000
y(i+1)=(t.*ydot(i))-(0.5*g*(t.^2));
p(i+1)=p0*(1-(2.333*10e-5)*y).^5;
t=t+dt
end
this is a small section of my code. y(i+1) works perfectly, but whenever i try and evaluate p(i+1), I keep getting, "In an assignment A(I) = B, the number of elements in B and I must be the same". All I want is there to be an array of p based on each value of y.
I would appreciate any help thanks.
  1 件のコメント
Jan
Jan 2013 年 4 月 2 日
編集済み: Jan 2013 年 4 月 2 日
I strongly recommend to search for this frequently asked question in this forum. This question is answered at least 5 times per week. Searching by your own before letting others create an answer is efficient also: Google or the search of this forum find corresponding answers in less than a second.
Please format you code properly. This time I've done this for you, but when you follow the "? help" link, you will learn more tricks about formatting in this forum.

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

採用された回答

Mahdi
Mahdi 2013 年 4 月 2 日
In the p(i+1) section, the y that you are multiplying by is actually a matrix.
I think there is a mistake there because you probably just want the current element, so this should solve your problem:
p(i+1)=p0.*(1-2.33.*10e-5.*y(i+1)).^5;

その他の回答 (1 件)

Jan
Jan 2013 年 4 月 2 日
編集済み: Jan 2013 年 4 月 2 日
The error message means, that you have a vector on the right, but a scalar on the left hand side for the assignment:
p(i+1) = p0*(1-(2.333*10e-5)*y).^5;
Here y is a vector, such that the expression of the right is a vector also. Perhaps you want (sorry, pure guessing, because you did not explain what the code shoudl achieve):
p(i+1) = p0 * (1 - 2.333e-5*y(i+1)) .^ 5;
"2.333e-5" is more efficient, because it does not need a multiplcation as in "2.333*10e-5".
Btw. please search for the term "pre-allocation" in this forum and the Matlab documentation. It helps to improve the speed dramatically.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by