find length of a line use 2 dots

5 Ansichten (letzte 30 Tage)
Rahma Yeni
Rahma Yeni am 23 Mär. 2012
Hello.. I have code like this
x = [0.2177, 0.6302, 0.9597, 0.6025];
y = [0.4284, 0.7822, 0.5073, 0.1564];
for m = 1:length(x)
for n = 1:length(x)
(n)= sqrt((x(:,n+m) - x(:,n)).^2 + (y(:,n+m) - y(:,n)).^2)
end
end
pjgtot = sum(pjg)
and the result are:
pjg =
0.5434
pjg =
0.5434 0.4291
pjg =
0.5434 0.4291 0.5007
??? Attempted to access x(:,5); index out of
bounds because size(x)=[1,4].
Error in ==> maze_solution at 322
pjg (n)= sqrt((x(:,n+m) - x(:,n)).^2 +
(y(:,n+m) - y(:,n)).^2)
>>
the result is true, that is :
pjg = 0.5434 0.4291 0.5007
but it give some error too.. why it happened .. is there something wrong with my code..?? manual calculations to obtain these results is (in this example, i use x = 0.2177, 0.6302, and y = 0.4284, 0.7822)
pjg = sqrt((0.6302-0.2177)^2 + (0.7822-0.4284)^2)
where is the fault..??
thank you.. :)

Akzeptierte Antwort

Jonathan Sullivan
Jonathan Sullivan am 23 Mär. 2012
dist = sum(sqrt(diff(x).^2+diff(y).^2))

Weitere Antworten (0)

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by