1
votes

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?

1
Your explanation was very clear, until you actually asked the question. What is the output you expect?KenHBS
yes it should be the same. But if i group all group 1 together, group 2 together and group 3 together, the position of data points in Matrix A will also change , so how do I know the exact data points for Matrix A if the order has been changed.DataMiningStudent
Maybe you can use names(groupings) <- seq(1, length(groupings)) so that the names indicate the row number in matrix AKenHBS

1 Answers

1
votes

I am not exactly sure what your expected output is, but maybe one of the following two options can help?

# sample data
A = as.matrix(read.table(text="1   1   
1   2   
2   1   
2   2   
10  1   
10  2   
11  1   
11  2   
5   5   
5   6 ",header=F))
B = c(2,   1,   5,   5,  10,   1)
B = matrix(B,3,byrow = T)

# compute minimum distance
distancematrix = t(apply(A, 1,function(y) {apply(B,1,function(x) {dist(rbind(x,y))})}))

# option 1
A_df = as.data.frame(A)
A_df$group = apply(distancematrix,1,which.min)
A_df[order(A_df$group),]

# option 2
split(as.data.frame(A),apply(distancematrix,1,which.min))

Output option 1:

   V1 V2 group
1   1  1     1
2   1  2     1
3   2  1     1
4   2  2     1
9   5  5     2
10  5  6     2
5  10  1     3
6  10  2     3
7  11  1     3
8  11  2     3

Output option 2:

$`1`
  V1 V2
1  1  1
2  1  2
3  2  1
4  2  2

$`2`
   V1 V2
9   5  5
10  5  6

$`3`
  V1 V2
5 10  1
6 10  2
7 11  1
8 11  2