1 What is this?

Short answer: various things I can never remember.

2 Code

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace arma;
using namespace Rcpp;

//##
//## Various utilities
//##

// [[Rcpp::export]]
arma::uvec uvec_1n(int n){
    arma::uvec out = arma::linspace<arma::uvec>(1, n, n);
    return out;
}
    
// [[Rcpp::export]]
arma::uvec complement_1n(int n, arma::uvec u, int shift=1){
    arma::uvec out = uvec_1n(n);
    out.shed_rows(u - shift);
    return out;
}

// [[Rcpp::export]]
arma::vec rep_nout(vec x, int nout){
    vec x_(nout);
    int k = 0;
    for (int i=0; i<nout; i++){
        x_(i) = x(k++);
        if (k == x.n_elem) k = 0;
    }
    return(x_);    
}

// [[Rcpp::export]]
arma::mat vec2mat(arma::vec x, int nrow, int ncol) {
    arma::vec x_ = rep_nout(x, nrow * ncol);
    arma::mat y(x_);
    y.set_size(nrow, ncol);
    return y;
}

//##
//## Remove, extract and replace elements from vectors
//##

// [[Rcpp::export]]
arma::vec remove_e(arma::vec X, arma::uvec ee, int shift=1) {
  X.shed_rows(ee - shift); // remove elements
  return(X);
}

// [[Rcpp::export]]
arma::vec extract_e(arma::vec X, arma::uvec ee, int shift=1) {
    return X.rows(ee - shift);
}

// [[Rcpp::export]]
arma::vec replace_e(arma::vec X, arma::uvec ee, arma::vec value, int shift=1) {
    X.rows(ee - shift) = value;
    return(X);
}


//##
//## Remove, extract and replace elements from matrices
//##


// [[Rcpp::export]]
arma::mat replace_r_c(arma::mat& M, arma::uvec& rr, arma::uvec& cc, arma::vec& value, int shift=1){
    arma::mat value_ = vec2mat(value, rr.n_elem, cc.n_elem);    
    M.submat(rr - shift, cc - shift) = value_;
    return(M);
}

// [[Rcpp::export]]
arma::mat replace_nr_nc(arma::mat M, arma::uvec rr, arma::uvec cc, arma::vec value, int shift=1){
    arma::uvec nr = complement_1n(M.n_rows, rr, shift=1);
    arma::uvec nc = complement_1n(M.n_cols, cc, shift=1);
    return replace_r_c(M, nr, nc, value, shift);
}

// [[Rcpp::export]]
arma::mat replace_r_nc(arma::mat M, arma::uvec rr, arma::uvec cc, arma::vec value, int shift=1){
    arma::uvec nc = complement_1n(M.n_cols, cc, shift=1);
    return replace_r_c(M, rr, nc, value, shift);
}

// [[Rcpp::export]]
arma::mat replace_nr_c(arma::mat M, arma::uvec rr, arma::uvec cc, arma::vec value, int shift=1){
    arma::uvec nr = complement_1n(M.n_rows, rr, shift=1);
    return replace_r_c(M, nr, cc, value, shift);
}

// [[Rcpp::export]]
arma::mat replace_r(arma::mat M, arma::uvec rr, arma::vec value, int shift=1){
    arma::uvec c = uvec_1n(M.n_cols);
    return replace_r_c(M, rr, c, value, shift);
}

// [[Rcpp::export]]
arma::mat replace_c(arma::mat M, arma::uvec cc, arma::vec value, int shift=1){
    arma::uvec r = uvec_1n(M.n_rows);
    return replace_r_c(M, r, cc, value, shift);
}

// [[Rcpp::export]]
arma::mat remove_r(arma::mat X, arma::uvec row_ent_, int shift=1) {
    X.shed_rows(row_ent_ - shift); // remove rows
    return(X);
}

// [[Rcpp::export]]
arma::mat extract_r(arma::mat X, arma::uvec row_ent_, int shift=1) {
    return (X.rows(row_ent_ - shift));
}

// [[Rcpp::export]]
arma::mat remove_c(arma::mat X, arma::uvec row_ent_, int shift=1) {
    X.shed_cols(row_ent_ - shift); // remove rows
    return(X);
}

// [[Rcpp::export]]
arma::mat extract_c(arma::mat X, arma::uvec row_ent_, int shift=1) {
    return (X.cols(row_ent_ - shift));
}

3 Examples

3.1 Utilities

uvec_1n(5)
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    3
## [4,]    4
## [5,]    5
complement_1n(5, c(2, 3))
##      [,1]
## [1,]    1
## [2,]    4
## [3,]    5
rep_nout(1:4, 15) |> as.numeric()
##  [1] 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
vec2mat(1:4, 3, 5)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    4    3    2    1
## [2,]    2    1    4    3    2
## [3,]    3    2    1    4    3

3.2 Remove/extract/replace elements from vector

vec <- 1:10
ee  <- c(3, 5, 8, 9)     ## Entries
vls <- c(-7, -5, -2, -3) ## Replacement values

## Remove elements
remove_e(vec, ee) |> as.numeric()
## [1]  1  2  4  6  7 10
## Extract elements
extract_e(vec, ee)  |> as.numeric()
## [1] 3 5 8 9
## Replace elements
replace_e(vec, ee, vls)  |> as.numeric()
##  [1]  1  2 -7  4 -5  6  7 -2 -3 10

3.3 Remove/extract/replace rows and cols from matrix

M <- matrix(1:16, nrow=4); M
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    2    6   10   14
## [3,]    3    7   11   15
## [4,]    4    8   12   16
rr <- c(1, 4)
cc <- c(4, 3)

## Remove rows / cols 
remove_r(M, rr)
##      [,1] [,2] [,3] [,4]
## [1,]    2    6   10   14
## [2,]    3    7   11   15
remove_c(M, cc)
##      [,1] [,2]
## [1,]    1    5
## [2,]    2    6
## [3,]    3    7
## [4,]    4    8
## Extract rows / cols
extract_r(M, rr)
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    4    8   12   16
extract_r(M, cc)
##      [,1] [,2] [,3] [,4]
## [1,]    4    8   12   16
## [2,]    3    7   11   15
## Replace rows / cols
replace_r(M, rr, c(-10, -20, -30))
##      [,1] [,2] [,3] [,4]
## [1,]  -10  -30  -20  -10
## [2,]    2    6   10   14
## [3,]    3    7   11   15
## [4,]  -20  -10  -30  -20
replace_c(M, cc, c(-10, -20, -30))
##      [,1] [,2] [,3] [,4]
## [1,]    1    5  -20  -10
## [2,]    2    6  -30  -20
## [3,]    3    7  -10  -30
## [4,]    4    8  -20  -10

3.4 Replace submatrices with values

Note: nc and nr should be read as “negative c” and “negative r”, i.e. as “-c” and “-r”.

rvls <- c(-99, -999, -9999)
rr <- c(1, 4)
cc <- c(4, 3)

replace_r_nc(M, rr, cc, rvls) 
##      [,1]  [,2] [,3] [,4]
## [1,]  -99 -9999    9   13
## [2,]    2     6   10   14
## [3,]    3     7   11   15
## [4,] -999   -99   12   16
replace_nr_c(M, rr, cc, rvls) 
##      [,1] [,2]  [,3] [,4]
## [1,]    1    5     9   13
## [2,]    2    6 -9999  -99
## [3,]    3    7   -99 -999
## [4,]    4    8    12   16
replace_nr_nc(M, rr, cc, rvls) 
##      [,1]  [,2] [,3] [,4]
## [1,]    1     5    9   13
## [2,]  -99 -9999   10   14
## [3,] -999   -99   11   15
## [4,]    4     8   12   16
replace_r_c(M, rr, cc, rvls)
##      [,1] [,2]  [,3] [,4]
## [1,]    1    5 -9999  -99
## [2,]    2    6    10   14
## [3,]    3    7    11   15
## [4,]    4    8   -99 -999