1

I am a beginner and now I faced a problem, I think it should have a very easy solution. Thank you for your help.

I have a matrix 313*442

each column should multiply with a fixed number in a separate column in the other data set.

Column one should multiply by 0.8, column two should multiply by -2.3 and ... and at the end, the sum of the row should calculate. in final I should have one column that should correspond to each row.

Sotos
  • 51,121
  • 6
  • 32
  • 66

2 Answers2

0

An option would be to replicate the column in the second dataset to make the lengths same and multiply with the matrix

m1 * df1$v1[col(m1)]
#    [,1] [,2]
#[1,]    2    9
#[2,]    4   12

Or another option is sweep

sweep(m1, 2,  df1$v1,  `*`)
#      [,1] [,2]
#[1,]    2    9
#[2,]    4   12

data

m1 <- matrix(c(1, 2, 3, 4), ncol = 2)
df1 <- data.frame(v1 = c(2, 3))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Assuming that m1 is your matrix and v1 the vector with the values to be multiplied by each column. Then,

m1 <- matrix(c(1, 2, 3, 4), ncol = 2)
m1
     [,1] [,2]
[1,]    1    3
[2,]    2    4

v1 <- c(2, 3)

t(t(m1) * v1)
#     [,1] [,2]
#[1,]    2    9
#[2,]    4   12
Sotos
  • 51,121
  • 6
  • 32
  • 66