MATLAB: How to find the average velocity across positions in coordinates using a for loop

coordinatesfor loop

I have two variables that both consist of column vectors with 3000 rows. One are X coordinates and the other are Y coordinates. I am trying to find the average velocity over a given time that these coordinates were collected. I figured a for loop would work best given the amount of data that is here. I am struggling to figure out how i can find the distance from position 2 to 1, 3 to 2, 4 to 3……3000 to 2999, and have this run in the for loop. P is currently just a place holder I have used to say position. Any help would be great thank you!
for i = 1:2999
P(i+1)-P(i);
end

Best Answer

Use sqrt()
distance = 0;
for k = 2 : length(x)
distance = distance + sqrt((x(k) - x(k-1))^2 + (y(k) - y(k-1))^2);
end
% Divide total distance by the number of time points to get the average velocity.
meanVelocity = distance / length(x);