-1

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.

  • "I need help" is not a question, because I cannot tell whether your issue is a mysterious exception or not knowing a word of the language (or something in between). Please [edit] your question to specify *precisely* what help you need. – Joe C Dec 30 '17 at 03:25
  • For a matrix, `matrix(mat, ncol = 2, byrow = TRUE)`. For a list, `split(mat, rep(seq(length(mat) / 2), each = 2))` – alistaire Dec 30 '17 at 03:39

1 Answers1

1

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.

Luke C
  • 10,081
  • 1
  • 14
  • 21
  • can I get the answer in the form of matrix? – DataMiningStudent Dec 31 '17 at 15:37
  • @DataMiningStudent - Sure, Trebek- but can you be more specific? Are you after a separate matrix for each row in your original matrix, or just one matrix (and if the latter, did try alistaire's comment answers above)? – Luke C Dec 31 '17 at 19:23
  • Hi, I want a separate matrix for each row in my original matrix. example, after one iteration i got one matrix so that i can use it in calculation. Then the next iteration another matrix and feed the answer for calculation. I hope I made my self clear. sorry if my explanation is confusing. – DataMiningStudent Jan 01 '18 at 12:43
  • I've edited my question to insert the draft codes for your reference – DataMiningStudent Jan 01 '18 at 13:08
  • @DataMiningStudent - I've made a few changes, but I'm still not exactly sure what you're trying to do- hopefully it helps. – Luke C Jan 02 '18 at 20:42
  • your solution is very helpful and yes I will take your suggestion to ask a new question with actual input and output so that I can explain clearly what I actually mean. Thank you so much for your solution – DataMiningStudent Jan 05 '18 at 07:38