Learn R Programming

popdemo (version 0.2-3)

Matlab2R: Read Matlab style matrices into R

Description

Read a matrix coded in a Matlab style into Rto create an object of class matrix

Usage

Matlab2R(M)

Arguments

M
an object of class character that represents a numeric matrix coded in a Matlab style.

Value

  • An object of class matrix.

Details

Matlab reads matrices using a unique one-line notation that can prove useful for storage in databases and importing multiple matrices into a program at once, amongst other applications. This notation is by row, with "[" and "]" to specify the beginning and end of the matrix respectively, ";" to specify a new row and a space between each matrix element. Thus, the Rmatrix created using matrix(c(0,1,2,0.5,0.1,0,0,0.6,0.6), byrow=TRUE, ncol=3) may is equivalent to [0 1 2;0.5 0.1 0;0 0.6 0.6]. Matlab2R takes a Matlab-coded matrix expressed as a character string and uses string-splitting and string-building techniques to convert it into an Robject of class matrix. As well as providing a simpler means of matrix notation in R, it also enables simultaneous import of multiple matrices of varying dimensions, using comma-seperated dataframes and tables.

See Also

R2Matlab

Examples

Run this code
# Create a 3x3 PPM using Matlab2R
    A<-Matlab2R("[0 1 2;0.5 0.1 0;0 0.6 0.6]")
    A

Run the code above in your browser using DataLab