greeting all the experts,
x=-2:1:2; %coordinates x
n=length(x);
y=x.^2; %coordinates y
z=[x;y];
from the above example, I will have matrix z (2x5)=[-2 -1 0 1 2: 4 1 0 1 4]. This will give 5 point on the graph which are (-2,4), (-1,1), (0,0),(1,1) (2,4). then, my next step is, i want to find the distance for each points.
distance 1 from (-2,4) to (-1,1)
distance 2 from (-1,1) to (0,0)
distance 3 from (0,0) to(1,1)
distance 4 from (1,1) to (2,4)
My problem/question, how to type the distance formula generally to conduct operation on each elements?
distance formula = sqrt((x2-x1)^2+(y2-y1)^2)

 채택된 답변

Matt J
Matt J 2022년 7월 29일
편집: Matt J 2022년 7월 29일

1 개 추천

x=-2:1:2; %coordinates x
n=length(x);
y=x.^2; %coordinates y
z=[x;y]
z = 2×5
-2 -1 0 1 2 4 1 0 1 4
interDistances=vecnorm(diff(z,1,2),2,1) %the result
interDistances = 1×4
3.1623 1.4142 1.4142 3.1623

댓글 수: 4

Noriham B
Noriham B 2022년 7월 29일
great...it seems to answer my problem. Very much appreaciate the prompt reply. I was type exactly as urs. And looks fine and no error. But, when I press run button, I got this message
Undefined function 'vecnorm' for input arguments
of type 'double'.
Error in MatJJ (line 10)
interDistances=vecnorm(diff(z,1,2),2,1); %the
result
Noriham B
Noriham B 2022년 7월 29일
Dear Prof/Dr./Sir Matt J, I got it already. I have to download the VecNorm file first right?Iwas re tried just now. I get the results as expected. Tqvm for introducing me to the VecNorm.
Walter Roberson
Walter Roberson 2022년 7월 29일
vecnorm needs r2017b .
We assume you have a new enough version of MATLAB as you did not enter a release when you created your question.
Noriham B
Noriham B 2022년 7월 29일
noted Prof/Dr./Sir Walter. I will try to get the newer version after this. Thanks :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Chunru
Chunru 2022년 7월 29일

1 개 추천

x=-2:1:2; %coordinates x
y=x.^2; %coordinates y
z=[x;y]
z = 2×5
-2 -1 0 1 2 4 1 0 1 4
d = diff(z, 1, 2) % diff along 2nd dim
d = 2×4
1 1 1 1 -3 -1 1 3
d = vecnorm(d) % distance
d = 1×4
3.1623 1.4142 1.4142 3.1623

댓글 수: 1

Noriham B
Noriham B 2022년 7월 29일
Dear Prof/Dr./Sir Chunru. Tqvm for the help. U also use vecnorm like Matt J. I will study more about vecnorm for future need. Tq also to MATLAB for giving us opportunity to ask for help in this platform.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Linear Algebra에 대해 자세히 알아보기

제품

태그

질문:

2022년 7월 29일

댓글:

2022년 7월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by