sort and group vectors in a matrix
조회 수: 3 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Star Strider
2021년 11월 17일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!