How to use a function for multiple sets of numbers with a single command?
Show older comments
For instance, I created a function to calculate the distance between 2 points with X,Y coordinates with this formula:
D=sqrt((X1-X2)^2+(Y1-Y2)^2)
Point nb. X Y
and I have this matrix: A = 1 2800 3500
2 4200 5300
3 5100 6200
- - -
- - -
n x y
The dimension of matrix A is ,,n" rows and 2 columns
Now, how I can apply this function to calculate distances between points 1&2, 2&3, 3&4 and so on, with a single command?Is it possible?
*Note: English is not my first language, and I may have not been very explicit
Accepted Answer
More Answers (1)
D=sqrt((A(2:end,2)-A(1:end-1,2)).^2+(A(2:end,3)-A(1:end-1,3)).^2)
5 Comments
Sebastian Ciuban
on 3 Apr 2013
Mahdi
on 3 Apr 2013
Is your matrix A as you've shown it? Or is it only 2 columns?
Matt Kindig
on 3 Apr 2013
What are the dimensions of A? Based on your example, it is a bit unclear.
Sebastian Ciuban
on 3 Apr 2013
Matt Kindig
on 3 Apr 2013
Then you can just modify Mahdi's code as:
D=sqrt((A(2:end,2)-A(1:end-1,2)).^2+(A(2:end,1)-A(1:end-1,1)).^2)
Categories
Find more on Logical 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!