Trying to get all results from a for loop into an array
1 回表示 (過去 30 日間)
古いコメントを表示
I researched how others did it on this forum, but what I could get to work fills the first cell of the array with []. I am just wondering how to try and get the starting position into the first cell. It could also have something to do with the if function .... I don't know enough to try and help!
Any advice on making my code neater would be amazing as well, I am very new to all of this!
The stuff in bold is inside my for loop, and is the bit that I am trying to get to work.
clc
clear
close all
format long G
% Given variables
xpos = 4.000; % Initial x position
ypos = 0.000; % Initial y position
t = [0.00,0.04,0.08,0.12];
xvel = [0.0000,-0.2478,-0.4943,-0.7384]; % x velocities
yvel = [4.0000, 3.9915, 3.9659, 3.9234]; % y velocities
count = 0;
% Find the position of x and y using the velocities
for a = 1:length(t)
count = count + 1 ;
if count-1 <= 0
continue
end
xpos = xvel(count).*(t(count) - t(count-1)) + xpos ;
xposa{a} = xpos;
ypos = yvel(count).*(t(count) - t(count-1)) + ypos ;
yposa{a} = ypos;
end
disp(xposa);
disp(yposa);
Thanks,
Ted Baker
0 件のコメント
回答 (1 件)
KSSV
2020 年 7 月 29 日
clc
clear
close all
format long G
% Given variables
xpos = 4.000; % Initial x position
ypos = 0.000; % Initial y position
t = [0.00,0.04,0.08,0.12];
xvel = [0.0000,-0.2478,-0.4943,-0.7384]; % x velocities
yvel = [4.0000, 3.9915, 3.9659, 3.9234]; % y velocities
% Find the position of x and y using the velocities
for a = 1:length(t)-1
xpos = xvel(a).*(t(a) - t(a+1)) + xpos ;
xposa{a} = xpos;
ypos = yvel(a).*(t(a) - t(a+1)) + ypos ;
yposa{a} = ypos;
end
disp(xposa);
disp(yposa);
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!