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

5 views (last 30 days)
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 Comment
Jan
Jan on 2 Apr 2013
Edited: Jan on 2 Apr 2013
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.

Sign in to comment.

Accepted Answer

Mahdi
Mahdi on 2 Apr 2013
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;

More Answers (1)

Jan
Jan on 2 Apr 2013
Edited: Jan on 2 Apr 2013
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.

Community Treasure Hunt

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

Start Hunting!