當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R語言 outer()用法及代碼示例


R 語言中的 outer() 函數用於將函數應用於兩個數組。

用法: outer(x, y, FUN=”*”, …)
參數: 
x, y: arrays 
FUN: function to use on the outer products, default value is multiply 
 

範例1:

Python3


# 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)

輸出:



     [, 1] [, 2] [, 3]
[1, ]    2    4    6
[2, ]    4    8   12
[3, ]    6   12   18
[4, ]    8   16   24
[5, ]   10   20   30

範例2:

Python3


# R program to illustrate
# outer function
 
# Initializing two arrays of elements
x <- c(1, 2, 3, 4, 5)
y<- c(2, 4, 6)
 
# Adding array x elements with array y elements
outer(x, y, "+")

輸出:

     [, 1] [, 2] [, 3]
[1, ]    3    5    7
[2, ]    4    6    8
[3, ]    5    7    9
[4, ]    6    8   10
[5, ]    7    9   11

相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Performing different Operations on Two Arrays in R Programming – outer() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。