sort and group vectors in a matrix

8 visualizaciones (últimos 30 días)
Avik Mahata
Avik Mahata el 17 de Nov. de 2021
Respondida: Star Strider el 17 de Nov. de 2021
I have two matrices of a size of 125x3. Lets say matrix X and Y. I am calculating cartesian distances between X(1,1) with all the rows of Y. So the code looks like below,
L = length(X);
for i=1:L
X(i)= sqrt((X(1,3)-Y(i,3))^2 + (X(1,4)-Y(i,4))^2 + (X(1,5)-Y(i,5))^2);
X = X/10;
B = sort(X);
B = B';
end
I am now trying to get the distances between all of X's elements with Y's element and sort and save them in a bigger matrix of 125x125. So I am trying to create another loop that saves the data in sucessive columns, but somehow I am not able to do that.

Respuestas (1)

Star Strider
Star Strider el 17 de Nov. de 2021
This is a bit confusing.
I would use the pdist2 function for this —
x = rand(1,5);
y = rand(10,5);
X = x(3:5)
X = 1×3
0.1281 0.5614 0.6284
Y = y(:,3:5);
D = pdist2(X, Y)
D = 1×10
0.4898 0.7704 0.8340 0.3424 1.0069 0.6262 0.5756 0.9131 0.5587 0.3489
[Dsort,I] = sort(D(:))
Dsort = 10×1
0.3424 0.3489 0.4898 0.5587 0.5756 0.6262 0.7704 0.8340 0.9131 1.0069
I = 10×1
4 10 1 9 7 6 2 3 8 5
The sort call sorts the vectors and returns the sorted vector and the original indices of the corresponding element.
I do not see where a matrix of distances would be used here, because this compares one vector to a matrix. A matrix would be appropriate for comparing two matrices, as described in Compute Euclidean Distance.
.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by