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!