summaryrefslogtreecommitdiff
path: root/macros/ipermute.sci
blob: ed78383d8d475825c5a377d0d89e601fdb8f6d9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
Description
    The inverse of the permute function.
    The expression
        ipermute (permute (A, perm), perm)
    returns the original array A.
Calling Sequence
    ipermute (A, iperm)
*/
function B = ipermute(A, perm)
    // ipermute : Inverse permute the dimensions of a matrix A.
    // B = ipermute(A, perm) returns the array A with dimensions inverted
    // according to the permutation vector `perm`.
    // Validate the permutation vector
    if max(size(perm)) ~= ndims(A) || or(gsort(perm, "g", "i") ~= 1:ndims(A))
        error('Permutation vector must contain unique integers from 1 to ndims(A).');
    end
    // Compute the inverse permutation vector
    invPerm = zeros(size(perm,1),size(perm , 2));
    for i = 1:max(size(perm))
        invPerm(perm(i)) = i;
    end
    // Use the permute function with the inverse permutation
    B = permute(A, invPerm);
endfunction