R中的外部()函数
R编程语言中的outer()函数 用于将函数应用于两个数组。
Syntax: outer(x, y, FUN=”*”, …)
Parameters:
- x, y: arrays
- FUN: function to use on the outer products, default value is multiply
R 编程示例中的 outer()函数
示例 1:两个向量的外积
R
# R program to illustrate
# outer function
# Initializing two arrays of elements
x <- c(1, 2, 3, 4, 5)
y<- c(2, 4, 6)
# Multiplying array x elements with array y elements
# Here multiply (*) parameter is not used still this
# function take it as default
outer(x, y)
R
# R program to illustrate
# outer function
# Initializing two arrays of elements
x <- 1:8
y<- 4
# Multiplying array x elements with array y elements
# Here multiply (*) parameter is not used still this
# function take it as default
outer(x, y, "+")
输出:
[, 1] [, 2] [, 3]
[1, ] 2 4 6
[2, ] 4 8 12
[3, ] 6 12 18
[4, ] 8 16 24
[5, ] 10 20 30
示例 2:向量和单值的外部函数
R
# R program to illustrate
# outer function
# Initializing two arrays of elements
x <- 1:8
y<- 4
# Multiplying array x elements with array y elements
# Here multiply (*) parameter is not used still this
# function take it as default
outer(x, y, "+")
输出:
[,1]
[1,] 5
[2,] 6
[3,] 7
[4,] 8
[5,] 9
[6,] 10
[7,] 11
[8,] 12