Matrix (version 0.99-3)

Matrix: Construct a Classed Matrix

Description

Construct a Matrix of a class that inherits from Matrix.

Usage

Matrix(data=NA, nrow=1, ncol=1, byrow=FALSE, dimnames=NULL,
       sparse = NULL)

Arguments

data
an optional numeric data vector or matrix.
nrow
the desired number of rows
ncol
the desired number of columns
byrow
logical. If FALSE (the default) the matrix is filled by columns, otherwise the matrix is filled by rows.
dimnames
a dimnames attribute for the matrix: a list of two character components.
sparse
logical or NULL, specifying if the result should be sparse or not. By default, it is made sparse when more than half of the entries are 0.

Value

  • Returns an nrow by ncol matrix of a class that inherits from "Matrix".

Details

If either of nrow or ncol is not given, an attempt is made to infer it from the length of data and the other parameter.

Although it is sometime possible to mix unclassed matrices (created with matrix) with ones of class "Matrix", it is much safer to always use carefully constructed ones of class "Matrix".

See Also

Matrix-class; matrix.

Examples

Run this code
Matrix(0, 3, 2)             # 3 by 2 matrix of zeros -> sparse
Matrix(0, 3, 2, sparse=FALSE)# forced 'dense'
Matrix(1:6, 3, 2)           # a 3 by 2 matrix
Matrix(1:6, nrow=3)
Matrix(1:6, ncol=2)
Matrix(1:9, nrow=3,
       dimnames = list(c("a", "b", "c"), c("A", "B", "C")))
Matrix(diag(3))# identity matrix (sparse)

Run the code above in your browser using DataLab