Learn R Programming

kStatistics (version 1.0)

pCart: Cartesian product

Description

Generates the Cartesian product between arrays

Usage

pCart( L )

Arguments

L

arrays in a list

Value

list

list with the Cartesian product

Details

The function pairs any element of the first array with any element of the second array, iteratively if there are more than two arrays in input. Repetitions are allowed. See examples.

References

Knuth, Donald E. (1998). The Art of Computer Programming. (3rd ed.). Addison Wesley.

See Also

mpCart

Examples

Run this code
# NOT RUN {
A <- c(1,2)
B <- c(3,4,5) 
pCart( list( A, B) ) 
# generate the Cartesian product [[1,3],[1,4],[1,5],[2,3],[2,4],[2,5]] 

L1<-list( c(1,1), c(2)) 
L2<-list( c(5,5), c(7) )
L3<-pCart ( list(L1, L2) )
# generate the Cartesian product [[1,1],[5,5]], [[1,1],[7]], [[2],[5,5]], [[2],[7]] 

pCart ( list(L3, c(7)) ) 
# generate the Cartesian product [[1,1],[5,5],[7]], [[1,1],[7],[7]], [[2],[5,5],[7]], [[2],[7],[7]] 

# }

Run the code above in your browser using DataLab