## Define a random non-negative matrix matrix
n <- 200; p <- 20; r <- 3
V <- matrix(runif(n*p), n, p)
## Compute the optimal matrix K for a given X matrix
X <- matrix(runif(n*r), n, r)
res <- fcnnls(X, V)
## Compute the same thing using the Moore-Penrose generalized pseudoinverse
res <- fcnnls(X, V, pseudo=TRUE)
## It also works in the case of single vectors
y <- runif(n)
res <- fcnnls(X, y)
# or
res <- fcnnls(X[,1], y)
Run the code above in your browser using DataLab