Skip to content

Instantly share code, notes, and snippets.

@jonocarroll
Created November 17, 2023 00:34
Show Gist options
  • Save jonocarroll/31ed49e2ec5df6aebee76ff08e258b42 to your computer and use it in GitHub Desktop.
Save jonocarroll/31ed49e2ec5df6aebee76ff08e258b42 to your computer and use it in GitHub Desktop.
rextendr Rust/R function to calculate the rowMins of a matrix (no NA processing)
library(rextendr)
rust_function("
fn rowmin(m: RMatrix::<i32>) -> Vec<i32> {
// convert to an ndarray
let matrix = <ArrayView2<i32>>::from_robj(&m).unwrap();
// store results
let mut min_values = Vec::with_capacity(matrix.len());
// iterate through each row in the matrix and find min
for row in matrix.rows() {
// find the minimum value in the current row
// and push it to the result vector
min_values.push(*row.iter().min().unwrap());
}
min_values
}
",
extendr_fn_options = list(use_try_from = TRUE),
features = "ndarray",
profile = "release"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment