Shuffle the rows and calculate the distance

2 vues (au cours des 30 derniers jours)
Awab Khalefa
Awab Khalefa le 7 Mai 2021
I wrote this code
m=input('Enter Number of rows: ');
n=input('Enter Number of Columns: ');
p = 3 ;
R = [0 10;20 30 ;50 60] ;
A = zeros(m,n) ;
for i = 1:p
idx = (i-1)*m/p+(1:m/p) ;
A(idx,:) = (R(i,2)-R(i,1))*rand(m/p,n)+R(i,1) ;
end
disp(A)
Then, by shuffling all rows (for example, row 1 will be the 10th row in the new matrix, or row 1 will be randomly moved to be the last row in the new matrix), each row will be distances with all other rows, and the distances between rows will be calculated for each respective row. it will be calculated according to the Euclidean relation to match the other row corresponding to it. After calculating the distance between each row and another row, the distance between them should be written in a new matrix. Thus, the distance results for all lines will be obtained. Finally, rows that are in the same group with each other, depending on their distance, will be displayed in a list.
I don't know how to do the last part. Can anyone help me please?

Réponses (1)

Image Analyst
Image Analyst le 8 Mai 2021
Modifié(e) : Image Analyst le 8 Mai 2021
Not exactly sure what you're asking but you can use pdist2() to compute the distance of every x,y coordinate point in a list to every other point in the list:
xy1 = rand(5, 2) % 5 (x,y) coordinates
xy1 = 5×2
0.5609 0.1033 0.9355 0.8045 0.9784 0.6512 0.2440 0.8867 0.8627 0.0957
distanceMatrix1 = pdist2(xy1, xy1)
distanceMatrix1 = 5×5
0 0.7950 0.6888 0.8450 0.3019 0.7950 0 0.1592 0.6964 0.7126 0.6888 0.1592 0 0.7712 0.5674 0.8450 0.6964 0.7712 0 1.0042 0.3019 0.7126 0.5674 1.0042 0
% Scramble
newOrder = randperm(size(xy1, 1));
xy2 = xy1(newOrder, :)
xy2 = 5×2
0.9784 0.6512 0.8627 0.0957 0.9355 0.8045 0.2440 0.8867 0.5609 0.1033
distanceMatrix2 = pdist2(xy2, xy2)
distanceMatrix2 = 5×5
0 0.5674 0.1592 0.7712 0.6888 0.5674 0 0.7126 1.0042 0.3019 0.1592 0.7126 0 0.6964 0.7950 0.7712 1.0042 0.6964 0 0.8450 0.6888 0.3019 0.7950 0.8450 0

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by