sort and group vectors in a matrix
5 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Star Strider
on 17 Nov 2021
This is a bit confusing.
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.
.
0 Comments
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!