Trying to find the Euclidean distance of 2 vectors with different sizes

4 次查看(过去 30 天)
T = [
5.1000 3.5000
6.4000 3.2000
5.4000 3.4000
5.7000 2.8000
5.7000 4.4000
5.6000 2.9000 ];
X = [
5.5000 3.8000
6.4000 2.9000
4.3000 3.0000
5.4000 3.0000
4.5000 1.5000
1.1000 0.1000
1.1000 0.1000 ];
I'm trying to find the euclidean distance between each row of T and each row of X.
This is the method that I have been using to find the euclidean distance and to store the results into an vector, within a nested for loop.
for i=1:1:size(T)
for j=1:1:size(X)
distance( i ,1 ) = sqrt( (( T( i , 1 ) - X ( j , 1 )).^2 ) + (( T( i , 2) - X( j , 2 )).^2) );
end
end

回答(1 个)

Torsten
Torsten 2022-11-5
T =[5.1000 3.5000
6.4000 3.2000
5.4000 3.4000
5.7000 2.8000
5.7000 4.4000
5.6000 2.9000 ];
X =[5.5000 3.8000
6.4000 2.9000
4.3000 3.0000
5.4000 3.0000
4.5000 1.5000
1.1000 0.1000
1.1000 0.1000 ];
D = pdist2(T,X)
D = 6×7
0.5000 1.4318 0.9434 0.5831 2.0881 5.2498 5.2498 1.0817 0.3000 2.1095 1.0198 2.5495 6.1400 6.1400 0.4123 1.1180 1.1705 0.4000 2.1024 5.4203 5.4203 1.0198 0.7071 1.4142 0.3606 1.7692 5.3339 5.3339 0.6325 1.6553 1.9799 1.4318 3.1385 6.2968 6.2968 0.9055 0.8000 1.3038 0.2236 1.7804 5.3000 5.3000

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by