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 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Torsten
el 10 de Mzo. de 2024
Movida: Torsten
el 10 de Mzo. de 2024
Use
x(x==0) = [];
instead of
for i = 1:56
if x(i) == 0
x(i) = []
end
end
The length of the x-vector will become shorter than 56 in the course of the loop such that x(56), e.g., will no longer exist when i = 56. This causes an access error.
0 comentarios
Walter Roberson
el 10 de Mzo. de 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 comentarios
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!