How to use a function for multiple sets of numbers with a single command?
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
D=sqrt((A(2:end,2)-A(1:end-1,2)).^2+(A(2:end,3)-A(1:end-1,3)).^2)
댓글 수: 5
Sebastian Ciuban
2013년 4월 3일
Mahdi
2013년 4월 3일
Is your matrix A as you've shown it? Or is it only 2 columns?
Matt Kindig
2013년 4월 3일
What are the dimensions of A? Based on your example, it is a bit unclear.
Sebastian Ciuban
2013년 4월 3일
Matt Kindig
2013년 4월 3일
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)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!