The outer product of the so-called array (or vector) A and B refers to a new element that each element of a and each element of B is multiplied together to obtain. Of course, the arithmetic rules can also be defined by themselves. The outer product operator is%o% (note: The letter in the middle of the percent semicolon is the lowercase letter O). For example:
> A <-1:2> b <-3:5> d <-a%o% b> d [, 1] [, 2] [, 3][1,] 3 4 5[2,] 6 8 10
Note that the dimension formula is:
Dim (d) = C (Dim (a), dim (b))
in fact, the R language provides a more generalized out-of-the-outer (x,y,fun), simpler, see a demo sample bar.
The following example is to draw a 3-dimensional surface plot of z = x**2 +50cos (y):
> x <-seq (from = -5, to = 5, by = 0.25) > y <-seq (from =-2, to = 2, by = 0.1) > F <-function (x, y) {x**2 +50*cos (y)}> z <-outer (x,y,f) > persp (x, Y, z)
Draw images such as the following:
See the Help documentation for more.
How to calculate the outer product of two arrays (or vectors) in R language