Learn R Programming

climtrends (version 1.0.6)

BSXFUN: Apply a function to each element of 2 arrays with singleton expansion enabled

Description

BSXFUN applies a function to each element of 2 arrays with singleton expansion enabled, clone of MATLAB's BSXFUN

Usage

BSXFUN(op, x, y)

Arguments

op
binary function to apply to arrays x and y
x
array of numeric or boolean values
y
array of numeric or boolean values

Value

applied to x and y

References

Douglas M. Schwarz, 2006 Generalized Array Operations http://www.mathworks.com/matlabcentral/fileexchange/10333-generalized-array-operations/content/genop.m

Examples

Run this code
#example 1
x<-matrix(c(17,24,1,8,15,23,5,7,14,16,4,6,13,20,22,10,12,19,21,3,11,18,25,2,9),
5,5,byrow=TRUE)
y<-matrix(13,1,5)
op<-'+'
BSXFUN(op,x,y)

#example 2
d1<-c(4,3,1,2)
x = array(runif(prod(d1)),d1)
d2<-c(4,1,5,2)
y = array(runif(prod(d2)),d2)
op<-'*'
BSXFUN(op,x,y)

#example 3
d1<-c(1,2,5,6,3)
x<-array(1:prod(d1),d1)
d2<-c(1,2,5,1,3,4)
y<-array(1:prod(d2),d2)
op<-'+'
BSXFUN(op,x,y)

Run the code above in your browser using DataLab