I am wanting to delete these entries of vector x when x = 0. How do I do this. Matlab gives me an error right now saying index cannot exceed 38 but I don't know what's wrong.
6 次查看(过去 30 天)
显示 更早的评论
clear all
for i = 10:1:65
x(i-9) = i;
theta = i;
tspan = linspace(0,1000,1000);
V = 60;
Vx = 60*cosd(theta);
Vy = 60*sind(theta);
options.Events = @event_func;
[t,f,te,fe,ie] = ode45(@(t,f) eqs_of_motion(t,f),tspan,[0 0 Vx Vy],options);
if fe(:,2) > 25
x(i-9) = x(i-9);
y(i-9) = fe(:,2);
else fe(:,2) <=25
x(i-9) = 0;
y(i-9) = 0;
end
end
for i = 1:56
if x(i) == 0
x(i) = []
end
end
0 个评论
回答(2 个)
Walter Roberson
2024-3-10
@Torsten's recommendation of x(x==0) = [] is the best. But alternately,
for i = 56:-1:1
if x(i) == 0
x(i) = []
end
end
Each time you delete an entry, the following entries "fall down" to fill the hole. If you are looping forward 1:56 then if even one entry gets deleted then x(56) will no longer exist and you get the error. If you loop backwards then you do not encounter the same problem.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!