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

5 次查看(过去 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 CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by