NumericMatrix
and LogicalMatrix
are two of the many matrix classes for different data types provided by R in Rcpp.
Rcpp is a package that integrates R and C++. It provides both R functions and C++ classes.
NumericMatrix
The NumericMatrix
is a type of matrix that takes a numerical vector value as a parameter, and the elements in it are double
.
Let’s discuss this with a code example below:
Rcpp::sourceCpp('/usercode/main.cpp')myNumericMatrix()
The code above is for the NumericMatrix
, where the elements of the matrix are numerical values.
Rcpp
library.Rcpp
into this code file.0
value, which is a double
data type in C++. By default, it initializes the matrix with all 0 entries.LogicalMatrix
On the other hand, we have LogicalMatrix
. In LogicalMatrix
, the function gets a logical vector where the elements are of the bool
data type.
Let’s discuss this in detail with the code example below.
Rcpp::sourceCpp('/usercode/main.cpp')myLogicalMatrix()
The code given above is for the LogicalMatrix
, where the elements of the matrix are logical values.
Rcpp
library.Rcpp
into this code file.FALSE
value which is of the bool
data type. By default, it initializes the matrix with all FALSE
entries.Note: There are two files in our code. One is an
.r
file namedmain.r
, and the other one is a.cpp
file namedmain.cpp
. The major part of code is written in the.cpp
file, while the.r
file calls the function. Both the files are mutually integrated with each other usingsourceCpp
.
Free Resources