sort and group vectors in a matrix
12 views (last 30 days)
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);
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';
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.
Star Strider on 17 Nov 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)
Y = y(:,3:5);
D = pdist2(X, Y)
[Dsort,I] = sort(D(:))
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.