% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cprod.R
\name{cprod}
\alias{cprod}
\title{Cartesian product of many vectors and matrices}
\usage{
cprod(..., rev = 1, as.data.frame = 0, rownames = 0, colnames = 0)
}
\arguments{
\item{...}{vectors, factors or matrices \cr
Or  ... can be a list containing the levels vectors as its elements.}

\item{rev}{= 0 to make the fist elements of ... change first}

\item{as.data.frame}{= 1 to make the result a data frame.}

\item{rownames}{= 1 to return the compressed version as the rownames.}

\item{colnames}{= 1 to use the names of ... as the colnames.}
}
\value{
A matrix or data frame.
}
\description{
Cartesian product of many vectors and matrices
}
\details{
When all the inputs are vectors, \code{cprod} is equivalent to
\code{expand.grid1}.\cr

When both \code{A} and \code{B} are matrices, 
\code{cprod( A, B )} is equivalent to
\preformatted{
  Reduce( rbind, lapply(as.data.frame(t(A))
       ,  function(x) cbind(repmat(matrix(x,1,ncol(A)),nrow(B)),B)) )
}
}
\examples{
cprod(1:2,3:5)
cprod(1:2,3:4, rev=0)
cprod(matrix(1:6,3,2, byrow=1),1:2)
cprod(matrix(1:6,3,2, byrow=1),matrix(1:4,2,2,byrow=1))

# when ... is a list
ll <- list(a=c("a","b"),bbb=1:3)
cprod(ll, colnames=0)
cprod(ll, colnames=1)

# ANOVA data
levels <- c(2,3)
ll <- lapply(levels,function(x)seq(1:x))
names(ll) <- c("A","B")
data <- cprod(ll, colnames=1)

# generate 3D grid of x0
x0 <- 1:3
ndim <- 3
x <- cprod( rep(list(x0),ndim) )
Print(x)

}
