Clear Filters
Clear Filters

How to repeat calculations and store answers

2 views (last 30 days)
I have a set of data that I want to fin the distance between two points for every point. d = ((x2-x1)^2 + (y2-y1)^2)^(1/2). Where x2 and y2 are in the next row and that d gets stored into another column vector. Does anybody have any suggestions for how to do this? I will include sample data that has x and y points.
  1 Comment
Bob Thompson
Bob Thompson on 20 Feb 2018
I'm sure somebody knows a better way, but what about a for loop?
for i=2:size(data,1)
d(i) = ((x(i)-x(i-1))^2+(y(i)-y(i-1))^2)^0.5;
end

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 20 Feb 2018
Edited: Stephen23 on 20 Feb 2018
You should write simple vectorized code, e.g.:
X = [-83.55705;-83.55705;-83.557078;-83.557078;-83.557078;-83.557105;-83.557105;-83.557133;-83.557133;-83.55716;-83.55716;-83.55716;-83.557188;-83.557188;-83.557215;-83.557215;-83.557243;-83.557272;-83.557272;-83.557272;-83.557298;-83.557298;-83.557327;-83.557327;-83.557353;-83.557353;-83.557353;-83.55738;-83.55738;-83.557408;-83.557408;-83.557435;-83.557435;-83.557463;-83.557463;-83.557463;-83.55749;-83.55749;-83.557517;-83.557517;-83.557543;-83.557543;-83.557543;-83.557572;-83.557572;-83.557598;-83.557598;-83.557625;-83.557625;-83.557652;-83.557652;-83.557652;-83.557678;-83.557678;-83.557703;-83.557703;-83.55773;-83.55773;-83.55773;-83.557757;-83.557757;-83.557783;-83.557783;-83.55781;-83.55781;-83.557837;-83.557837;-83.557837;-83.557862;-83.557862;-83.557888;-83.557888;-83.557915;-83.557915;-83.557915;-83.557942;-83.557942;-83.557968;-83.557968;-83.557995;-83.557995;-83.557995;-83.558022;-83.558022;-83.558048;-83.558048;-83.558073;-83.558073;-83.5581;-83.5581;-83.5581;-83.558127;-83.558127;-83.558153;-83.558153;-83.55818;-83.55818;-83.55818;-83.558207;-83.558207;-83.558232;-83.558232;-83.558258;-83.558258;-83.558285;-83.558285;-83.558285;-83.55831;-83.55831;-83.558337;-83.558337;-83.558363;-83.558363;-83.558363;-83.558388;-83.558388;-83.558415;-83.558415;-83.558442;-83.558442;-83.558467;-83.558467;-83.558493;-83.558493;-83.558493;-83.558518;-83.558518;-83.558545;-83.55857;-83.55857;-83.55857;-83.558595];
Y = [40.302548;40.302548;40.302577;40.302577;40.302577;40.302605;40.302605;40.302633;40.302633;40.30266;40.30266;40.30266;40.30269;40.30269;40.302718;40.302718;40.302747;40.302775;40.302775;40.302775;40.302803;40.302803;40.302832;40.302832;40.30286;40.30286;40.30286;40.302888;40.302888;40.302917;40.302917;40.302945;40.302945;40.302973;40.302973;40.302973;40.303002;40.303002;40.30303;40.30303;40.303058;40.303058;40.303058;40.303087;40.303087;40.303115;40.303115;40.303143;40.303143;40.303172;40.303172;40.303172;40.3032;40.3032;40.303228;40.303228;40.303257;40.303257;40.303257;40.303285;40.303285;40.303313;40.303313;40.303342;40.303342;40.303372;40.303372;40.303372;40.303398;40.303398;40.303428;40.303428;40.303457;40.303457;40.303457;40.303485;40.303485;40.303513;40.303513;40.303542;40.303542;40.303542;40.30357;40.30357;40.3036;40.3036;40.303628;40.303628;40.303657;40.303657;40.303657;40.303683;40.303683;40.303713;40.303713;40.30374;40.30374;40.30374;40.303768;40.303768;40.303797;40.303797;40.303825;40.303825;40.303853;40.303853;40.303853;40.303882;40.303882;40.30391;40.30391;40.303938;40.303938;40.303938;40.303967;40.303967;40.303997;40.303997;40.304025;40.304025;40.304055;40.304055;40.304083;40.304083;40.304083;40.304113;40.304113;40.304142;40.30417;40.30417;40.30417;40.304198];
D = sqrt((X(2:end)-X(1:end-1)).^2 + (Y(2:end)-Y(1:end-1)).^2)

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!