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 CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by