Compute pairwise difference between matrix columns

pairwise_col_diff(x)

Arguments

x

A data matrix of size n times p. Where rows are observations and columns are features.

Value

A matrix of size n times (p choose 2), where each column is the difference between two of the original columns.

Examples

n = 1
p = 4
x = matrix(rep(1:p, n), nrow = n, ncol = p, byrow = TRUE)
colnames(x) = paste0("X", 1:p)
pairwise_col_diff(x)
#>      X1--X2 X1--X3 X1--X4 X2--X3 X2--X4 X3--X4
#> [1,]     -1     -2     -3     -1     -2     -1