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

1 vue (au cours des 30 derniers jours)
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 commentaire
Jan
Jan le 2 Avr 2013
Modifié(e) : Jan le 2 Avr 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.

Connectez-vous pour commenter.

Réponse acceptée

Mahdi
Mahdi le 2 Avr 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;

Plus de réponses (1)

Jan
Jan le 2 Avr 2013
Modifié(e) : Jan le 2 Avr 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.

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by