Subset Rcpp Matrix by Logical Condition in R (4 Examples)
We show you how to subset Rcpp matrices by logical vectors in R. Furthermore, we show you how to apply logical matrix subsetting with RcppArmadillo matrices. We have extra blog entries for the Rcpp and RcppArmadillo package in case you are not familiar with them.
The structure is as follows:
Let the sub-setting begin!
Example 1: Standard Rcpp Matrix, Subset of Rows
Load the Rcpp package.
if (!require('Rcpp', quietly = TRUE)) { install.packages('Rcpp') } library('Rcpp') # Load package 'Rcpp'
With Rcpp, we create C++ function “fun_subset_mat”. The function takes two inputs, a matrix “Input_Matrix” and a logical vector “Input_Log_Vec”. It returns a matrix with those rows of “Input_Matrix” for which the corresponding entry in “Input_Log_Vec” is true. Therefore, the length of “Input_Log_Vec” has to equal the number of rows of “Input_Matrix”.
cppFunction(' Rcpp::NumericMatrix fun_subset_mat( Rcpp::NumericMatrix Input_Matrix, Rcpp::LogicalVector Input_Log_Vec) { // Get the number of rows and columns of "Input_Matrix" int Mat_nrows = Input_Matrix.nrow(); int Mat_ncols = Input_Matrix.ncol(); // Define matrix "Output_Mat" with the aimed for dimensions, // i.e. with the number of rows corresponding to the length of "Input_Log_Vec" Rcpp::NumericMatrix Output_Mat(sum(Input_Log_Vec), Mat_ncols); // Loop over the entries of "Input_Log_Vec" to see whether the // corresponding row of "Input_Matrix" should be included in // the output matrix "Output_Mat" for (int i = 0, j = 0; i < Mat_nrows; i++) { if (Input_Log_Vec[i]) { Output_Mat(j, _) = Input_Matrix(i, _); j = j+1; } } // Return the output matrix "Output_Mat" return(Output_Mat); } ')
Test the function:
Input_Matrix = matrix(1:15, nrow = 3) Input_Matrix # [,1] [,2] [,3] [,4] [,5] # [1,] 1 4 7 10 13 # [2,] 2 5 8 11 14 # [3,] 3 6 9 12 15 Input_Log_Vec = c(TRUE, FALSE, TRUE) Input_Log_Vec # [1] TRUE FALSE TRUE fun_subset_mat(Input_Matrix, Input_Log_Vec) # [,1] [,2] [,3] [,4] [,5] # [1,] 1 4 7 10 13 # [2,] 3 6 9 12 15
Example 2: Standard Rcpp Matrix, Subset of Columns
Similarly, we can also define a function subsetting the columns of a matrix.
cppFunction(' Rcpp::NumericMatrix fun_subset_mat_cols( Rcpp::NumericMatrix Input_Matrix, Rcpp::LogicalVector Input_Log_Vec) { // Get the number of rows and columns of "Input_Matrix" int Mat_nrows = Input_Matrix.nrow(); int Mat_ncols = Input_Matrix.ncol(); // Define matrix "Output_Mat" with the aimed for dimensions, // i.e. with the number of columns corresponding to the length of "Input_Log_Vec" Rcpp::NumericMatrix Output_Mat(Mat_nrows, sum(Input_Log_Vec)); // Loop over the entries of "Input_Log_Vec" to see whether the // corresponding column of "Input_Matrix" should be included in // the output matrix "Output_Mat" for (int i = 0, j = 0; i < Mat_ncols; i++) { if (Input_Log_Vec[i]) { Output_Mat(_, j) = Input_Matrix(_, i); j = j+1; } } // Return the output matrix "Output_Mat" return(Output_Mat); } ')
Test the function:
Input_Matrix = matrix(1:15, nrow = 3) Input_Matrix # [,1] [,2] [,3] [,4] [,5] # [1,] 1 4 7 10 13 # [2,] 2 5 8 11 14 # [3,] 3 6 9 12 15 Input_Log_Vec = c(TRUE, FALSE, TRUE, FALSE, FALSE) Input_Log_Vec # [1] TRUE FALSE TRUE FALSE FALSE fun_subset_mat_cols(Input_Matrix, Input_Log_Vec) # [,1] [,2] # [1,] 1 7 # [2,] 2 8 # [3,] 3 9
Example 3: RcppArmadillo Matrix, Subset of Rows
The RcppArmadillo library is especially useful for linear algebra. We therefore additionally show the above example with the Armadillo library. You can find our log post about RcppArmadillo here.
cppFunction(depends = "RcppArmadillo", ' arma::mat fun_arma_mat_rows( arma::mat input_mat, arma::vec input_vec ) { return input_mat.rows(find(input_vec == 1)); } ')
Well, that was short. Let us test the function:
Input_Matrix = matrix(1:15, nrow = 3) Input_Matrix # [,1] [,2] [,3] [,4] [,5] # [1,] 1 4 7 10 13 # [2,] 2 5 8 11 14 # [3,] 3 6 9 12 15 Input_Log_Vec = c(TRUE, FALSE, TRUE) Input_Log_Vec # [1] TRUE FALSE TRUE fun_arma_mat_rows(Input_Matrix, Input_Log_Vec) # [,1] [,2] [,3] [,4] [,5] # [1,] 1 4 7 10 13 # [2,] 3 6 9 12 15
Example 4: RcppArmadillo Matrix, Subset of Columns
For getting a subset of columns of an Armadillo matrix, we can apply the following function.
cppFunction(depends = "RcppArmadillo", ' arma::mat fun_arma_mat_cols( arma::mat Input_Matrix, arma::vec Input_Log_Vec ) { return Input_Matrix.cols( find(Input_Log_Vec == 1) ); } ')
Test the function:
Input_Matrix = matrix(1:15, nrow = 3) Input_Matrix # [,1] [,2] [,3] [,4] [,5] # [1,] 1 4 7 10 13 # [2,] 2 5 8 11 14 # [3,] 3 6 9 12 15 Input_Log_Vec = c(TRUE, FALSE, TRUE, FALSE, FALSE) Input_Log_Vec # [1] TRUE FALSE TRUE FALSE FALSE fun_arma_mat_cols(Input_Matrix, Input_Log_Vec) # [,1] [,2] # [1,] 1 7 # [2,] 2 8 # [3,] 3 9
Video & Further Resources
For more general information on programming, you may want to have a look at the following introductory lecture for programming methodology, published on the Stanford YouTube channel.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
We published many more R and Python programming posts at https://statisticsglobe.com/, take a look:
- Speed Up Loop Using Rcpp Package in R (Example)
- sugar Functions in Rcpp Package in R (Example)
- Matrix in Rcpp & RcppArmadillo Packages in R (2 Examples)
- Convert Vector from RcppArmadillo to Rcpp & Vice Versa in R (2 Examples)
- Learn R Programming (Tutorial & Examples)
This post showed you how to subset matrices by logical vectors in Rcpp. If you have any questions or comments, please use the options below.
This page was created in collaboration with Anna-Lena Wölwer. Have a look at Anna-Lena’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.
Statistics Globe Newsletter