Learn R Programming

kStatistics (version 2.0)

pCart: Cartesian product

Description

The function returns the cartesian product between vectors.

Usage

pCart( L )

Arguments

L

vectors in a list

Value

list

list with the cartesian product

Details

The function pairs any element of the first vector with any element of the second vector, iteratively if there are more than two vectors 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) 
# Return the cartesian product [[1,3],[1,4],[1,5],[2,3],[2,4],[2,5]]
pCart( list( A, B) ) 
 

L1<-list( c(1,1), c(2)) 
L2<-list( c(5,5), c(7) )
# Return the Cartesian product [[1,1],[5,5]], [[1,1],[7]], [[2],[5,5]], [[2],[7]]
# assign the value to L3
L3<-pCart ( list(L1, L2) )
 
# Return the Cartesian product between L3 and [7]
# The result will be [[1,1],[5,5],[7]], [[1,1],[7],[7]], [[2],[5,5],[7]], [[2],[7],[7]]
pCart ( list(L3, c(7)) ) 

# }

Run the code above in your browser using DataLab