diff options
Diffstat (limited to 'macros/ipermute.sci')
-rw-r--r-- | macros/ipermute.sci | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/macros/ipermute.sci b/macros/ipermute.sci new file mode 100644 index 0000000..ed78383 --- /dev/null +++ b/macros/ipermute.sci @@ -0,0 +1,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 |