I have two matrices that is Matrix A and Matrix B
Matrix A:
[,1][,2]
[1,] 1 1
[2,] 1 2
[3,] 2 1
[4,] 2 2
[5,] 10 1
[6,] 10 2
[7,] 11 1
[8,] 11 2
[9,] 5 5
[10,] 5 6
Matrix B:
[,1][,2][,3][,4][,5][,6]
[1,] 2 1 5 5 10 1
For every row in Matrix A, I calculate the euclidean distance to every two columns in Matrix B.
For example, to get the answer for the following in result matrix:
[,1]
[1,]
The calculation is:
A(1,1) - From Matrix A
B(2,1) - From Matrix B
= sqrt((xA -xB)^2 + (yA -yB)^2)
= sqrt((1-2)^2 + (1-1)^2)
= 1.00
xA and yA from Matrix A
xB and yB from Matrix B
To get the answer for the following in result matrix:
[,2]
[1,] 5.66
The calculation is:
A(1,1) - From Matrix A
B(5,5) - From Matrix B
= sqrt((xA -xB)^2 + (yA -yB)^2)
= sqrt((1-5)^2 + (1-5)^2)
= 5.66
To get the answer for the following in result matrix:
[,3]
[1,] 9.00
The calculation is:
A(1,1) - From Matrix A
B(10,1) - From Matrix B
= sqrt((xA -xB)^2 + (yA -yB)^2)
= sqrt((1-10)^2 + (1-1)^2)
= 9.00
After I have all the distances, I stored it in a distance matrix as per bleow:
Distance matrix (the answer for the euclidean distance):
[1,] [,2] [,3]
[1,] 1.00 5.66 9.00
[2,] 1.00 1.41
[3,]
[4,]
[5,]
[7,]
[8,]
[9,]
[10]
Then I do groupings based on the minimum distance on each row to know if each row belongs to group 1, 2 or 3. there are 3 groups all together.For example if i get the below groups, how to recover back the data points from matrix A?
> groupings <- apply(distanceMatrix, 1, which.min)
> [1] 1 1 1 1 3 2 3 2 1 1
For example the first four rows belongs to group 1, the 5th point belong to group 3, and so on. But if I re-arrange the answer and group all group 1 together, group 2 together and group 3 together, the position of Matrix A will change. so how can i get back the point from Matrix A correctly?
names(groupings) <- seq(1, length(groupings))
so that the names indicate the row number in matrix A – KenHBS