How to use a function for multiple sets of numbers with a single command?
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (1)
D=sqrt((A(2:end,2)-A(1:end-1,2)).^2+(A(2:end,3)-A(1:end-1,3)).^2)
5 commentaires
Sebastian Ciuban
le 3 Avr 2013
Mahdi
le 3 Avr 2013
Is your matrix A as you've shown it? Or is it only 2 columns?
Matt Kindig
le 3 Avr 2013
What are the dimensions of A? Based on your example, it is a bit unclear.
Sebastian Ciuban
le 3 Avr 2013
Matt Kindig
le 3 Avr 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)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!