Vectorisation of a loop

1 view (last 30 days)
Michael Ng
Michael Ng on 25 Aug 2020
Edited: Michael Ng on 25 Aug 2020
Is it possible to vectorise this loop? Any help would be greatly appreciated.
% Calculate X(k)
for k = 1:length(T)
X(k) = (sum(Vx(1:k-1).*diff(T(1:k))))+ X1;
end
% Calculate Y(k)
for k = 1:length(T)
Y(k) = (sum(Vy(1:k-1).*diff(T(1:k))))+ Y1;
end

Accepted Answer

Stephen23
Stephen23 on 25 Aug 2020
Edited: Stephen23 on 25 Aug 2020
X = X1+[0;cumsum(Vx(1:end-1).*diff(T))]
Y = Y1+[0;cumsum(Vy(1:end-1).*diff(T))]
  1 Comment
Michael Ng
Michael Ng on 25 Aug 2020
Thank you very much for your solution.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!