简体   繁体   中英

extract every two elements in matrix row in r in sequence to calculate euclidean distance

How to extract every two elements in sequence in a matrix and return the result as a matrix so that I could feed the answer in a formula for calculation:

For example, I have a one row matrix with 6 columns:

    [,1][,2][,3][,4][,5][,6]
[1,]   2   1   5   5  10   1

I want to extract column 1 and two in first iteration, 3 and 4 in second iteration and so on. The result has to be in the form of matrix.

[1,] 2  1    
[2,] 5  5
[3,] 10 1

My original codes:

     data <- matrix(c(1,1,1,2,2,1,2,2,5,5,5,6,10,1,10,2,11,1,11,2), ncol = 2)

Center Matrix:

     [,1][,2][,3][,4][,5][,6]
[1,]   2   1   5   5  10   1
[2,]   1   1   2   1  10   1
[3,]   5   5   5   6  11   2
[4,]   2   2   5   5  10   1
[5,]   2   1   5   6  5    5
[6,]   2   2   5   5  11   1
[7,]   2   1   5   5  10   1
[8,]   1   1   5   6  11   1
[9,]   2   1   5   5  10   1
[10,]  5   6   11  1  10   2

     objCentroidDist <- function(data, centers) {
       resultMatrix <- matrix(NA, nrow=dim(data)[1], ncol=dim(centers)[1])
       for(i in 1:nrow(centers)) {
          resultMatrix [,i] <- sqrt(rowSums(t(t(data)-centers[i, ])^2))
       }
          resultMatrix 
     }

    objCentroidDist(data,centers)

I want the Result matrix to be as per below:

        [1,][,2][,3]

    [1,]
    [2,]
    [3,]
    [4,]
    [5,]
    [7,]
    [8,]
    [9,]
    [10]

My concern is, how to calculate the data-centers distance if the dimensions of the data matrix are two, and centers matrix are six. (to calculate the distance from the data matrix and every two columns in centers matrix). Each row of the centers matrix has three centers.

Something like this maybe?

m <- matrix(c(2,1,5,5,10,1), ncol = 6)

list.seq.pairs <- lapply(seq(1, ncol(m), 2), function(x) {
  m[,c(x, x+1)]
})

> list.seq.pairs
[[1]]
[1] 2 1

[[2]]
[1] 5 5

[[3]]
[1] 10  1

And, in case you're wanting to iterate over multiple rows in a matrix, you can expand on the above like this:

mm <- matrix(1:18, ncol = 6, byrow = TRUE)       

apply(mm, 1, function(x) {
  lapply(seq(1, length(x), 2), function(y) {
    x[c(y, y+1)]
  })
})

EDIT:

I'm really not sure what you're after exactly. I think, if you want each row transformed into a 2 x 3 matrix:

mm <- matrix(1:18, ncol = 6, byrow = TRUE)       

list.mats <- lapply(1:nrow(mm), function(x){
  a = matrix(mm[x,], ncol = 2, byrow = TRUE)
})

> list.mats
[[1]]
     [,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6

[[2]]
     [,1] [,2]
[1,]    7    8
[2,]    9   10
[3,]   11   12

[[3]]
     [,1] [,2]
[1,]   13   14
[2,]   15   16
[3,]   17   18

If, however, you want to get to your results matrix- I think it's probably easiest to do whatever calculations you need to do while you're dealing with each row:

results <- t(apply(mm, 1, function(x) {
  sapply(seq(1, length(x), 2), function(y) {
    val1 = x[y] # Get item one
    val2 = x[y+1] # Get item two
    val1 / val2 # Do your calculation here
  })
}))

> results
          [,1]   [,2]      [,3]
[1,] 0.5000000 0.7500 0.8333333
[2,] 0.8750000 0.9000 0.9166667
[3,] 0.9285714 0.9375 0.9444444

That said, I don't understand what you're trying to do so this may miss the mark. You may have more luck if you ask a new question where you show example input and the actual expected output that you're after, with the actual values you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM