operation on single elements in MATLAB
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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)
0 Commenti
Risposta accettata
Matt J
il 29 Lug 2022
Modificato: Matt J
il 29 Lug 2022
x=-2:1:2; %coordinates x
n=length(x);
y=x.^2; %coordinates y
z=[x;y]
interDistances=vecnorm(diff(z,1,2),2,1) %the result
4 Commenti
Walter Roberson
il 29 Lug 2022
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.
Più risposte (1)
Chunru
il 29 Lug 2022
x=-2:1:2; %coordinates x
y=x.^2; %coordinates y
z=[x;y]
d = diff(z, 1, 2) % diff along 2nd dim
d = vecnorm(d) % distance
Vedere anche
Categorie
Scopri di più su Detection in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!