Learn R Programming

mgwrsar (version 1.3.2)

reord_M_R: reord_M

Description

Reorders the elements of a matrix row by row according to a corresponding index matrix. Each row of the index matrix specifies the new column order for the corresponding row of the input matrix.

Usage

reord_M_R(M, idxG)

Value

A reordered numeric matrix with the same number of rows as `M` and columns equal to the maximum index in `idxG`.

Arguments

M

A numeric matrix to be reordered.

idxG

An integer matrix of the same number of rows as `M`, where each row contains column indices indicating the new ordering for that row.

Details

This function performs a scatter-like reordering: for each row `i`, the values of `M[i, ]` are placed in the positions specified by `idxG[i, ]`. It is particularly useful for spatial or neighborhood-based rearrangements where each observation has its own local indexing of neighbors.

Examples

Run this code
M <- matrix(1:9, nrow = 3, byrow = TRUE)
idxG <- matrix(c(3, 1, 2, 1, 2, 3, 2, 3, 1), nrow = 3, byrow = TRUE)
reord_M_R(M, idxG)

Run the code above in your browser using DataLab