Hi I need help with for loop
3 ビュー (過去 30 日間)
古いコメントを表示
Hi I'm very new to matlab and was wondering if there was a way to code all of this within just one for loop
0 件のコメント
採用された回答
KSSV
2020 年 8 月 12 日
編集済み: KSSV
2020 年 8 月 12 日
This single loop is fine enough:
N = length(v_x) ;
xpos = zeros(N-1,1) ;
xpos(1) = 4 ;
for i = 2:N-1
xpos(i) = v_x(i)*(t(i)-t(i-1))+xpos(i-1) ;
endfor
3 件のコメント
KSSV
2020 年 8 月 12 日
There is a typo error....edited the answer. There was one extra paranthesis '('
その他の回答 (1 件)
Akira Agata
2020 年 8 月 12 日
No need to use for-loop. How about the following way?
% Read data file
T1 = readtable('A1_input.txt');
% Postion of (x,y) at time = 0
x0 = 4;
y0 = 0;
% Calculate the position for each time step
xPos = cumtrapz(T1.time, T1.vx) + x0;
yPos = cumtrapz(T1.time, T1.vx) + y0;
参考
カテゴリ
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!