Home > Net >  How do I add the output of a matrix in a for loop in R
How do I add the output of a matrix in a for loop in R

Time:10-10

I want to subtract the minimum value in each row from each row and subtract the minimum value in each column from each column and then add the result to make another matrix. When I do this, it gives me

Warning message: In a[i, ] a[, k] : longer object length is not a multiple of shorter object length How can I do add the result of the two output to get a matrix? This is my code

a <- matrix(c(2, 3, 6, 2, 4, 11, 6, 7, 2, 4, 13, 12), nrow= 4)
a



results <- matrix(nrow= length(a) , ncol= length(a))

for(k in 1:ncol(a)) {       # for-loop over rows
  a[, k] <- a[, k] - min(a[, k])
  results <- a[, k]
  #print(a[, k])
  print(results)
}

result2 <- matrix(nrow= length(a) , ncol= length(a))

for(i in 1:nrow(a)) {       # for-loop over rows
  a[i, ] <- a[i, ] -min(a[i,])
  result2 <- a[i,]
  #result2 <- a[i, ]   a[i, ]
  #print(a[i, ])
  print(result2)
}

CodePudding user response:

I wasn't 100% clear on what you were doing so I provided two attempts.

If you are looking to first edit the original matrix by subtracting the row minimums, and THEN subtract the new column minimums, see the code below:

# Load initial data
(a  <- matrix(c(2, 3, 6, 2, 4, 11, 6, 7, 2, 4, 13, 12), nrow= 4))
#     [,1] [,2] [,3]
#[1,]    2    4    2
#[2,]    3   11    4
#[3,]    6    6   13
#[4,]    2    7   12

# use apply to get the minimum for each row (index = 1)
(a_row_mins <- apply(a, 1, min))
#[1] 2 3 6 2

# straightforward matrix minus vector of equal row length
(b <- a - a_row_mins)
#     [,1] [,2] [,3]
#[1,]    0    2    0
#[2,]    0    8    1
#[3,]    0    0    7
#[4,]    0    5   10

(b_col_mins <- apply(b, 2, min))
#[1] 0 0 0

# less straightforward, but to make it straightforward, 
# turn the columns into rows using transpose and subtract like earlier
# use transpose again to set the matrix back to its original orientation
(output <- t(t(b) - b_col_mins))
#     [,1] [,2] [,3]
#[1,]    0    2    0
#[2,]    0    8    1
#[3,]    0    0    7
#[4,]    0    5   10

If you didn't want to actually use the new column minimums (they were all zero) you would do something like this instead:

# use apply to get the minimum for each row (index = 1)
(a_row_mins <- apply(a, 1, min))
#[1] 2 3 6 2
(a_col_mins <- apply(a, 2, min))
#[1] 2 4 2

# repeat row min for each column
(row_min_matrix <- replicate(ncol(a), a_row_mins))
#     [,1] [,2] [,3]
#[1,]    2    2    2
#[2,]    3    3    3
#[3,]    6    6    6
#[4,]    2    2    2

# repeat column min for each row
(col_min_matrix <- t(replicate(nrow(a), a_col_mins)))
#     [,1] [,2] [,3]
#[1,]    2    4    2
#[2,]    2    4    2
#[3,]    2    4    2
#[4,]    2    4    2
 
# add together
(row_min_plus_col_min <- row_min_matrix   col_min_matrix)
#     [,1] [,2] [,3]
#[1,]    4    6    4
#[2,]    5    7    5
#[3,]    8   10    8
#[4,]    4    6    4
 
# subtract from original
(output <- a - row_min_plus_col_min)
#     [,1] [,2] [,3]
#[1,]   -2   -2   -2
#[2,]   -2    4   -1
#[3,]   -2   -4    5
#[4,]   -2    1    8

CodePudding user response:

Your code is close, maybe 90% there. Here I make minor edits:

a <- matrix(c(2, 3, 6, 2, 4, 11, 6, 7, 2, 4, 13, 12), nrow=4)

results1 <- matrix(nrow=4, ncol=3)

for(k in 1:ncol(a)) {  
  results1[,k] <- a[,k] - min(a[,k])
}

results2 <- matrix(nrow=4, ncol=3)

for(i in 1:nrow(a)) {        
  results2[i,] <- a[i,] - min(a[i,])
}

results1 results2
     [,1] [,2] [,3]
[1,]    0    2    0
[2,]    1   15    3
[3,]    4    2   18
[4,]    0    8   20
  • Related