Hi I need help with for loop

조회 수: 3 (최근 30일)
Benedict Comerford
Benedict Comerford 2020년 8월 12일
댓글: Benedict Comerford 2020년 8월 12일
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

채택된 답변

KSSV
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
KSSV 2020년 8월 12일
There is a typo error....edited the answer. There was one extra paranthesis '('
Benedict Comerford
Benedict Comerford 2020년 8월 12일
Thank you so much that was a great help

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Akira Agata
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;
  댓글 수: 1
Benedict Comerford
Benedict Comerford 2020년 8월 12일
HI Akira
Thanks for that Sadly one of the requirements for my code is that it must be a for loop.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by