permuteMatrix

Permutes the rows or columns of a matrix.

Synopsis

permuteMatrix (a, permutation, permute)

Required Arguments

float a[[]] (Input)
Matrix of size nRows × nColumns to be permuted.
int permutation[] (Input)
Array of length n containing the permutation permutation[0], …, permutation[n-1] of the integers 0, …, n, where n = nRows if the rows of a are to be permuted and n = nColumns if the columns of a are to be permuted.
int permute (Input)
Keyword of type int. Argument permute must be either PERMUTE_ROWS, if the rows of a are to be interchanged, or PERMUTE_COLUMNS, if the columns of a are to be interchanged.

Return Value

Array of size nRows × nColumns containing the permuted input matrix a.

Description

Function permuteMatrix interchanges the rows or columns of a matrix using a permutation vector. The function permutes a column (row) at a time using function permuteVector. This process is continued until all the columns (rows) are permuted. On completion, let B = result and \(p_i= permutation \left[i\right]\), then \(B_{ij}=A_{pij}\) for all i, j.

Example

This example permutes the columns of a matrix a.

from pyimsl.stat.permuteMatrix import permuteMatrix, PERMUTE_COLUMNS
from pyimsl.stat.writeMatrix import writeMatrix

a = [[3.0, 5.0, 1.0, 2.0, 4.0],
     [3.0, 5.0, 1.0, 2.0, 4.0],
     [3.0, 5.0, 1.0, 2.0, 4.0]]
permutation = [2, 3, 0, 4, 1]

output = permuteMatrix(a, permutation, PERMUTE_COLUMNS)

writeMatrix("permuted matrix", output,
            rowNumberZero=True, colNumberZero=True)

Output

 
                          permuted matrix
             0            1            2            3            4
0            1            2            3            4            5
1            1            2            3            4            5
2            1            2            3            4            5