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.
1 view (last 30 days)
Show older comments
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 Comments
Answers (2)
Walter Roberson
on 10 Mar 2024
@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 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!