Index exceeds matrix dimensions error message
1 次查看(过去 30 天)
显示 更早的评论
I'm trying to use a while loop to create vectors so that I can plot the values but I keep getting the "Index exceeds matrix dimensions" error message. Any help is appreciated. This is my code:
figure(1)
z1=.6;
z2=.85;
z3=1.23;
V=sqrt(2*9.81*(z3-z1));
i=1;
t=0;
%%
while z3(i) >=0
x(i)=t*V;
Z3(i)=z3-.5*9.81*(t^2);
t=t+.25;
i=i+1;
end
0 个评论
回答(1 个)
KALYAN ACHARJYA
2019-11-13
编辑:KALYAN ACHARJYA
2019-11-13
Yoi defined z3 as scalar, but in the while loop you trying acess it as vectors
#Defined here
z3=1.23;
#
while z3(i)>=0
%...
end
Here waht does z3(i) means. In Matlab z3(i)
%.................................................................^ means array indexing
See the following example
>> Z3=4;
>> Z3(1)
ans =
4
>> Z3(2)
Index exceeds matrix dimensions.
>>
When "i" incremented within while loop, in the next iteration, how does z3 gets the value when i is greater than "i", like z3(2),z4(3)..so on.
Hope it helps, any issue let me know.
2 个评论
KALYAN ACHARJYA
2019-11-13
You have to figure out, it seems simple
z1=.6;
z2=.85;
z3=1.23;
V=sqrt(2*9.81*(z3-z1));
i=1;
t=0;
while z3>=0
x(i)=t*V;
z3=z3-0.5*9.81*(t^2);
t=t+.25;
i=i+1;
end
Do the proper notbook calculation and show me, so that I can help you Matlab implementation.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!