0

I have a matrix of 3 columns and 56 rows. Now I want to perform an operation on each of the (i,j) elements in this matrix and update the value in another pre-defined matrix of 3 columns and 56 rows.

A little snippet of my data is

miss_geno_probs
              [,1]         [,2]         [,3]
 [1,] 2.186745e-02 2.936486e-02 9.996548e-03
 [2,] 3.131395e-02 4.205017e-02 1.431495e-02
 [3,] 3.253137e-02 4.368498e-02 1.487148e-02
 [4,] 3.158590e-02 4.241535e-02 1.443927e-02
 [5,] 1.240662e-02 1.666031e-02 5.671596e-03
 [6,] 3.447492e-02 4.629489e-02 1.575996e-02

I want to perform a simple operation on each of the elements thus:

w1 <- miss_geno_probs[1,1]/sum(miss_geno_probs[1,])
> w1
[1] 0.3571429

This is what I tried but it wouldn't give me my desired output:

 missing_geno_weights<- matrix(rep(0, length(y_mis) * 3), ncol = 3)
    miss_geno_probs<- matrix(rep(0, length(y_mis) * 3), ncol = 3)
    for (i in seq_along(marginals)) {
      for (j in 0:2){
        miss_geno_probs[i,j+1]<- marginals[i] * geno_prop[j+1]
        missing_geno_weights[i,j+1]<- miss_geno_probs[i,j+1]/sum(miss_geno_probs[i,])
      }
    }

After trying agin, I have broken this down into 2 for loops and the computation is correct for the first line but only computing for the first row.

missing_geno_weights<- matrix(rep(0, length(marginals) * 3), ncol = 3)
>     for (i in 1:length(marginals)){
      for (j in seq_along(1:3)){
        missing_geno_weights[i, j]<- miss_geno_probs[i,j]/sum(miss_geno_probs[i,])
     }
    }
> missing_geno_weights
           [,1]      [,2]      [,3]
 [1,] 0.3571429 0.4795918 0.1632653
 [2,] 0.3571429 0.4795918 0.1632653
 [3,] 0.3571429 0.4795918 0.1632653
 [4,] 0.3571429 0.4795918 0.1632653
 [5,] 0.3571429 0.4795918 0.1632653
monif064
  • 13
  • 2
  • is that R? might be good to tag the question with the programming language you are using – Matthias Apr 13 '23 at 09:31
  • Yes it is, I have made edits to the original question. – monif064 Apr 13 '23 at 14:50
  • I don't know about R, but in python for example to iterate over each element in a matrix top to bottom you would write `for i in len(rows): for j in len(cols): modify(matrix[i, j])` does that not work here? Seems you have some weird sizes for the i and j in your for loops. – Matthias Apr 13 '23 at 15:08
  • also might help to see what the correct output is and what the wrong output is you are getting and/or any error messages – Matthias Apr 13 '23 at 15:09
  • There are actually no error messages. The matrix if just a small matrix with 56 rows and 3 columns. Each of the rows computed should be different. In the output i posted, the rows are the same meaning the code is only selecting the first row of the matrix miss_geno_prob for the operation hence the same output all over for the for loop computation. – monif064 Apr 13 '23 at 16:22

1 Answers1

0

Hello I adapted your code by renaming matrices but the desired output is here :

INP <- abs(rnorm(9,0,0.09)) # INPUT DATA , an example you replace by your OWN data or your intial matrix 'miss_geno_probs'
INP <- matrix(INP,nrow = 3,ncol = 3) # PUT IN MATRIX  fill in bycol by default
OUT <- matrix(NA,nrow = 3,ncol = 3) # CREATE OUTPUT MATRIX


#PERFORM FOR LOOP
for(row in 1:3){
  for(col in 1:3){
    OUT[row,col] <- INP[row,col]/sum(INP[,col]) # APPLY DESIRED OPERATION
  }
}