Title: | Useful Tricks for Matrix Manipulation |
---|---|
Description: | Provides functions, which make matrix creation conciser (such as the core package's function m() for rowwise matrix definition or runifm() for random value matrices). Allows to set multiple matrix values at once, by using list of formulae. Provides additional matrix operators and dedicated plotting function. |
Authors: | Krzysztof Joachimiak [aut, cre]
|
Maintainer: | Krzysztof Joachimiak <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.8.2 |
Built: | 2025-03-03 04:03:41 UTC |
Source: | https://github.com/krzjoa/matricks |
Extract or replace the antidiagonal of a matrix, or construct a antidiagonal matrix.
antidiag(x = as.numeric(c(1)), nrow = NULL, ncol = NULL) antidiag(x) <- value
antidiag(x = as.numeric(c(1)), nrow = NULL, ncol = NULL) antidiag(x) <- value
x |
matrix, vector or 1D array, or missing. |
nrow |
number of rows (optional; when x is not a matrix) |
ncol |
number of columns (optional; when x is not a matrix) |
value |
either a single value or a vector of length equal to that of the current antidiagonal. Should be of a mode which can be coerced to that of x. |
# Extracting antidiag antidiag(diag(3)) # Creating antidiagonal matrix antidiag(7, 3, 3) antidiag(1:5, 3, 3) # Assigning antidiagonal mat <- matrix(0, 3, 3) antidiag(mat) <- c(3, 4, 5) mat
# Extracting antidiag antidiag(diag(3)) # Creating antidiagonal matrix antidiag(7, 3, 3) antidiag(1:5, 3, 3) # Assigning antidiagonal mat <- matrix(0, 3, 3) antidiag(mat) <- c(3, 4, 5) mat
This function allows to access matrix values by passing indices as vector
at(mat, idx) at(mat, idx) <- value
at(mat, idx) at(mat, idx) <- value
mat |
matrix |
idx |
two-element integer vector |
value |
a value to be assign at index |
'at' function: value from matrix at index idx
mat <- matrix(0, 3, 3) idx <- c(1, 2) # Typically, given matrix and row-column indices as two-element vector, we should do it like this: mat[idx[1], idx[2]] mat[idx[1], idx[2]] <- 8 # Using `at`, we can do it simplier! at(mat, idx) at(mat, idx) <- 7 mat at(mat, idx)
mat <- matrix(0, 3, 3) idx <- c(1, 2) # Typically, given matrix and row-column indices as two-element vector, we should do it like this: mat[idx[1], idx[2]] mat[idx[1], idx[2]] <- 8 # Using `at`, we can do it simplier! at(mat, idx) at(mat, idx) <- 7 mat at(mat, idx)
This functions works very similar to well-known base 'cbind' or 'rbind' function. However, there is one big difference between these functions. If you pass a vector, each value will be get individually.
col_bind(...) row_bind(...)
col_bind(...) row_bind(...)
... |
single values, vectors, matrices or data.frames |
a matrix being a product of matrix/vector/values binding
# `col_bind` vs `cbind` cbind(1,2,3,4,5) col_bind(1,2,3,4,5) cbind(1:5) col_bind(1:5) cbind(matrix(3, 3, 3), 0.33, 4:7) col_bind(matrix(3, 3, 3), 0.33, 4:7) # `row_bind` vs `rbind` rbind(1,2,3,4,5) row_bind(1,2,3,4,5) rbind(1:5) row_bind(1:5) rbind(matrix(3, 3, 3), 0.33, 4:7) row_bind(matrix(3, 3, 3), 0.33, 4:7)
# `col_bind` vs `cbind` cbind(1,2,3,4,5) col_bind(1,2,3,4,5) cbind(1:5) col_bind(1:5) cbind(matrix(3, 3, 3), 0.33, 4:7) col_bind(matrix(3, 3, 3), 0.33, 4:7) # `row_bind` vs `rbind` rbind(1,2,3,4,5) row_bind(1,2,3,4,5) rbind(1:5) row_bind(1:5) rbind(matrix(3, 3, 3), 0.33, 4:7) row_bind(matrix(3, 3, 3), 0.33, 4:7)
Is idx possible in given matrix?
is_idx_possible(mat, idx)
is_idx_possible(mat, idx)
mat |
matrix |
idx |
two-element vector |
is_idx_possible(matrix(0, 3, 3), c(4, 5)) is_idx_possible(matrix(0, 3, 3), c(3, 2))
is_idx_possible(matrix(0, 3, 3), c(4, 5)) is_idx_possible(matrix(0, 3, 3), c(3, 2))
One of the main functionalities of the package. It is an alternative to standard way we define matrices in R.
m(...)
m(...)
... |
Single values, vectors, matrices and '|' as special symbol which breaks input on the rows. |
matrix with defines elements
# Typically, we define matrices like this: x <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow=3, byrow=TRUE) x # However, this way of ceating matices seems to be # a little bit clunky. Using `matricks`, we can do # it in more staightforward way dividing our input # into rows by using special symbol `|` x <- m(1, 2, 3| 4, 5, 6| 7, 8, 9) x # Moreover, we can pass to the `m` function # whole sequences or even matrices. x <- m(1:5 | 6:10 | 11:15 ) x # We can combine multiple matrices into one m(diag(3), diag(3) * 3| diag(3) * 3, diag(3) )
# Typically, we define matrices like this: x <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow=3, byrow=TRUE) x # However, this way of ceating matices seems to be # a little bit clunky. Using `matricks`, we can do # it in more staightforward way dividing our input # into rows by using special symbol `|` x <- m(1, 2, 3| 4, 5, 6| 7, 8, 9) x # Moreover, we can pass to the `m` function # whole sequences or even matrices. x <- m(1:5 | 6:10 | 11:15 ) x # We can combine multiple matrices into one m(diag(3), diag(3) * 3| diag(3) * 3, diag(3) )
Get available marix indices
matrix_idx(mat, n.row = NULL, n.col = NULL, mask = NULL)
matrix_idx(mat, n.row = NULL, n.col = NULL, mask = NULL)
mat |
matrix |
n.row |
number of rows; default: NULL |
n.col |
number of columns; default: NULL |
mask |
logical matrix; default: NULL |
T <- TRUE; F <- FALSE mat <- matrix(0, 3, 3) mask <- m(T, T, F | T, F, T | F, F, T) # All poss matrix_idx(mat) matrix_idx(mat, mask = mask) matrix_idx(mask = mask)
T <- TRUE; F <- FALSE mat <- matrix(0, 3, 3) mask <- m(T, T, F | T, F, T | F, F, T) # All poss matrix_idx(mat) matrix_idx(mat, mask = mask) matrix_idx(mask = mask)
Get all indices in neighbourhood
neighbour_idx(mat, idx, mask = NULL, diagonal = TRUE, include.idx = FALSE)
neighbour_idx(mat, idx, mask = NULL, diagonal = TRUE, include.idx = FALSE)
mat |
matrix or data.frame |
idx |
two-element vector |
mask |
logical matrix; optional |
diagonal |
include diagonal neighbours |
include.idx |
include current index |
mat <- matrix(0, 3, 3) neighbour_idx(mat, c(1, 2)) neighbour_idx(mat, c(1, 2), diagonal = FALSE) neighbour_idx(mat, c(1, 2), diagonal = FALSE, include.idx = TRUE) # With mask mat <- matrix(0, 3, 4) mask <- m(FALSE, FALSE, TRUE, TRUE | FALSE, FALSE, FALSE, FALSE | TRUE, TRUE, FALSE, TRUE) neighbour_idx(mat, c(1, 2), mask = mask)
mat <- matrix(0, 3, 3) neighbour_idx(mat, c(1, 2)) neighbour_idx(mat, c(1, 2), diagonal = FALSE) neighbour_idx(mat, c(1, 2), diagonal = FALSE, include.idx = TRUE) # With mask mat <- matrix(0, 3, 4) mask <- m(FALSE, FALSE, TRUE, TRUE | FALSE, FALSE, FALSE, FALSE | TRUE, TRUE, FALSE, TRUE) neighbour_idx(mat, c(1, 2), mask = mask)
Create matrix of lists, where each one contains list of neighbour field coordinates
neighbour_idx_matrix(mat, mask = NULL, diagonal = TRUE, random.select = NULL)
neighbour_idx_matrix(mat, mask = NULL, diagonal = TRUE, random.select = NULL)
mat |
matrix |
mask |
logical matrix. Its dimensions must be identical with dimensions of mat |
diagonal |
logical. get diagonal neighbours |
random.select |
select one random neighbour |
T <- TRUE; F <- FALSE mat <- matrix(0, 3, 3) mask <- m(T, T, F | T, F, T | F, F, T) nimat <- neighbour_idx_matrix(mat, mask, diagonal = TRUE) neighbour_idx_matrix(mat, mask, diagonal = TRUE, random.select = 1)
T <- TRUE; F <- FALSE mat <- matrix(0, 3, 3) mask <- m(T, T, F | T, F, T | F, F, T) nimat <- neighbour_idx_matrix(mat, mask, diagonal = TRUE) neighbour_idx_matrix(mat, mask, diagonal = TRUE, random.select = 1)
Create matrix with ones values only
ones(nrow, ncol)
ones(nrow, ncol)
nrow |
number of rows |
ncol |
number of columns |
ones(4, 5)
ones(4, 5)
This operator allows to do elementwise operation of two algebraic object i.e. matrices/vectors. There is one required condition to perform such operation: at least one domension values from both objects must be the same
a %m% b a %d% b a %-% b a %+% b
a %m% b a %d% b a %-% b a %+% b
a |
matrix/vector |
b |
matrix/vector |
Matrix/vector
# Multiply m(1, 2, 3 | 4, 5, 6 | 7, 8, 9) %m% v(5,4,3) # Divide m(1, 2, 3 | 4, 5, 6 | 7, 8, 9) %d% v(5,4,3) # Add m(1, 2, 3 | 4, 5, 6 | 7, 8, 9) %+% v(5,4,3) # Subtract m(1, 2, 3 | 4, 5, 6 | 7, 8, 9) %-% v(5,4,3)
# Multiply m(1, 2, 3 | 4, 5, 6 | 7, 8, 9) %m% v(5,4,3) # Divide m(1, 2, 3 | 4, 5, 6 | 7, 8, 9) %d% v(5,4,3) # Add m(1, 2, 3 | 4, 5, 6 | 7, 8, 9) %+% v(5,4,3) # Subtract m(1, 2, 3 | 4, 5, 6 | 7, 8, 9) %-% v(5,4,3)
This function allows us to plot matrices easily
plot_matrix(x, ...) ## S3 method for class 'matrix' plot(x, ...)
plot_matrix(x, ...) ## S3 method for class 'matrix' plot(x, ...)
x |
a matrix |
... |
for S3 generic API consistency; does nothing |
a ggplot object
T <- TRUE; F <- FALSE x1 <- m(T, T, T, F, T | T, T, F, T, T | F, T, T, T, F | T, T, T, T, T | F, F, T, T, T | F, T, T, T, F) plot_matrix(x1) x2 <- m(T, T, T, F, T | T, T, F, T, T ) plot(x2) x3 <- m(runif(3) | runif(3) | runif(3)) plot(x3)
T <- TRUE; F <- FALSE x1 <- m(T, T, T, F, T | T, T, F, T, T | F, T, T, T, F | T, T, T, T, T | F, F, T, T, T | F, T, T, T, F) plot_matrix(x1) x2 <- m(T, T, T, F, T | T, T, F, T, T ) plot(x2) x3 <- m(runif(3) | runif(3) | runif(3)) plot(x3)
Create matrix of random choosen boolean values
rboolm(nrow, ncol, true.proba = 0.5)
rboolm(nrow, ncol, true.proba = 0.5)
nrow |
number of rows |
ncol |
numer of columns |
true.proba |
probability of true values; default: 0.5 |
a matrix
rboolm(3, 3) rboolm(4, 5, true.proba = 0.3)
rboolm(3, 3) rboolm(4, 5, true.proba = 0.3)
Repeat matrix object respectively to its shape and orientation
crep(x, times) rrep(x, times)
crep(x, times) rrep(x, times)
x |
matrix |
times |
number of repetitions |
crep = columnwise repetition
rrep = rowwise repetition
matrix
# Columnwise repetition crep(v(1:3), 4) crep(t(v(1:5)), 4) # Rowwise repetition rrep(v(1:3), 4) rrep(t(v(1:5)), 4)
# Columnwise repetition crep(v(1:3), 4) crep(t(v(1:5)), 4) # Rowwise repetition rrep(v(1:3), 4) rrep(t(v(1:5)), 4)
Create matrix of random values with dimensions copied from an existing matrix
runif_same_dims(mat, min = 0, max = 1)
runif_same_dims(mat, min = 0, max = 1)
mat |
matrix |
min |
lower limit of the distribution. Must be finite. |
max |
upper limit of the distribution. Must be finite. |
a matrix
mat <- matrix(0, 3, 3) runif_same_dims(mat)
mat <- matrix(0, 3, 3) runif_same_dims(mat)
Create matrix of random values drawn from uniform distribution
runifm(nrow, ncol, min = 0, max = 1)
runifm(nrow, ncol, min = 0, max = 1)
nrow |
number of rows |
ncol |
numer of columns |
min |
lower limit of the distribution. Must be finite. |
max |
upper limit of the distribution. Must be finite. |
a matrix
runifm(3, 3) runifm(4, 5, min = -1, max = 3)
runifm(3, 3) runifm(4, 5, min = -1, max = 3)
Facilitates iterating over matrix, returning a sequence of pairs, where the first element is a value at index (x, y) and the second one is the index (x, y)
seq_matrix(mat)
seq_matrix(mat)
mat |
matrix |
list of two-element list (single value, two-element vector)
mat <- matrix(1:9, 3, 3) seq_matrix(mat)
mat <- matrix(1:9, 3, 3) seq_matrix(mat)
This functions allows to set multiple elements of a matrix instead of using annoying step-by-step assignment by mat[1,2] <- 2 mat[2,3] <- 0.5 etc.
set_values(mat, ...) sv(mat, ...)
set_values(mat, ...) sv(mat, ...)
mat |
a matrix object |
... |
formulae; left hand values should be two-element interger vectors and right-hand: a single-value numeric |
matrix
mat <- matrix(0, 4, 5) set_values(mat, c(1,1) ~ 5, c(3, 4) ~ 0.3)
mat <- matrix(0, 4, 5) set_values(mat, c(1,1) ~ 5, c(3, 4) ~ 0.3)
This function provides convenient shortcut to create a vertical (column) vector.
v(...)
v(...)
... |
arbitrary number of values |
matrix with dims n_elements x 1
# Enumerating all the values with commas v(1, 2, 3) # Passing whole sequence as an argument v(1:5)
# Enumerating all the values with commas v(1, 2, 3) # Passing whole sequence as an argument v(1:5)
Create new matrix copying dimensions from the existing one
with_same_dims(mat, data)
with_same_dims(mat, data)
mat |
a matrix with desired dimensions |
data |
sigle numeric value or numeric vector |
a matrix
x <- matrix(7, 3, 6) x with_same_dims(x, 0) with_same_dims(x, c(1, 2))
x <- matrix(7, 3, 6) x with_same_dims(x, 0) with_same_dims(x, c(1, 2))
Create matrix with zeros values only
zeros(nrow, ncol)
zeros(nrow, ncol)
nrow |
number of rows |
ncol |
number of columns |
zeros(4, 5)
zeros(4, 5)