0

Calculate sequence score based on score matrix.

sum(j[k])


j <- matrix(1:25, ncol = 5, nrow = 5)
diag(j) <- 0
j

n <- 1:5
k <- sample(n, 5, replace = FALSE)
k <- replicate(5, sample(n, 5, replace = FALSE))

j is score matrix. k is sequence type matrix.

lets say k[1,] = 4 1 5 3 2 
         k[2,] = 2 5 4 2 4

solution: Please help answer two issues;

Issue 1:

add one more column to matrix k (lets call it "score"). Based on J matrix the score for this sequence should be 48.

4 1 5 3 2 48 

Issue 2:

k[2,] = 2 5 4 2 4 The sample function is producing wrong permutations. I don't want any repetition in the sequence. Here 4 is repeated. Secondly 1 is missing. is there any other best way to generate random permutations.

  • Can you elaborate on how that score of 48 came about? – AkselA Sep 06 '19 at 11:47
  • if you cross ref k[1,] with j matrix: – Jon Dongen Sep 06 '19 at 12:05
  • 4 1: value in j is 4, similarly 1 5=21, 5 3 = 15 and 3 2 = 8 (we sum up: 4+21+15+8 = 48) – Jon Dongen Sep 06 '19 at 12:08
  • In the beginning of yout code `j` and `k` are not defined ... (later) `n` is not defined. Please use `set.seed(..)` to have replicable data. – jogo Sep 06 '19 at 12:21
  • jogo, thanks. j and k are clearly defined. I have added n = 1:5. I'm not looking for exact but conceptual solution. Once you have generated a sample of length 5 (lets call it, a sequence)- how can one calculate the sum of sequence from score matrix. – Jon Dongen Sep 06 '19 at 12:31
  • Please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jogo Sep 06 '19 at 13:42
  • 1
    Can you please give an example of your input and output ? – Vikrant Sep 06 '19 at 14:39

1 Answers1

1

You better double check the result. Without a reproducible example from your end it's difficult to confirm the values.

set.seed(1)
k <- replicate(5, sample(5))

# each column is a random permutation of 1:5
k
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    2    5    2    3    5
# [2,]    5    4    1    5    1
# [3,]    4    2    3    4    2
# [4,]    3    3    4    1    4
# [5,]    1    1    5    2    3

j <- matrix(1:25, 5)
diag(j) <- 0

nr <- nrow(k)

# arrange successive values as a column pair
ix <- cbind(c(k[-nr,]), c(k[-1,]))

# use the column pair to reference indices in j
jx <- j[ix]

# arrange j-values into a matrix and sum by column, producing the scores
scores <- colSums(matrix(jx, nr-1))

cbind(t(k), scores)
#                scores
# [1,] 2 5 4 3 1     59
# [2,] 5 4 2 3 1     44
# [3,] 2 1 3 4 5     55
# [4,] 3 5 4 1 2     53
# [5,] 5 1 2 4 3     42
AkselA
  • 8,153
  • 2
  • 21
  • 34
  • Hi Aksel, unfortunately it doesn't work. lets pick first row of k: 2 5 2 3 5 (according to j: 2-5: 22, 5-2:10, 2-3: 12, 3-5:23 so sum should be 22+10+12+23 = 67). Your solution suggests 59. – Jon Dongen Sep 06 '19 at 16:28
  • `k` is transposed relative to yours (hence the comment "each column is a random permutation of 1:5"), as it makes the calculations easier. The scores should match `t(k)`. – AkselA Sep 06 '19 at 16:56