1

The question is very simple. How to multiply every element of vector A by single element of vector B.

Here is the example:

> a
[1] 5 5 5 5 5 5 5 5

> b
[1] 250  252  252

The final result should return one object which looks similar to:

1   1250 1250 1250 1250 1250 1250 1250 1250
2   1260 1260 1260 1260 1260 1260 1260 1260
3   1260 1260 1260 1260 1260 1260 1260 1260

I've spent some time by trying do this by function for but I couldn't get proper result.

Some examples how to achieve that will be perfect answer.

Thanks.

Egel
  • 1,796
  • 2
  • 24
  • 35
  • This looks like you're going for a matrix multiplication (row vector times a column vector), and it looks like R supports matrix multiplication: http://stat.ethz.ch/R-manual/R-patched/library/base/html/matmult.html – dvntehn00bz Apr 10 '14 at 03:31
  • 2
    `outer(b,a)` or `b %o% a` – thelatemail Apr 10 '14 at 03:35

1 Answers1

4

Try using tcrossprod

> tcrossprod(b,a)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1250 1250 1250 1250 1250 1250 1250 1250
[2,] 1260 1260 1260 1260 1260 1260 1260 1260
[3,] 1260 1260 1260 1260 1260 1260 1260 1260

You can also use %*% as in: b %*% t(a)

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138