What does the ipermute function do in MATLAB / RunMat?
ipermute(A, order) reverses the dimension reordering performed by permute(A, order).
It is equivalent to permute(A, invOrder), where invOrder(order(i)) = i.
How does the ipermute function behave in MATLAB / RunMat?
ordermust be a permutation of1:n, wheren = numel(order).- The length of
ordermust be at leastndims(A); missing axes are treated as trailing singleton dimensions. - Duplicate, zero, or negative indices raise an error.
- Works with numeric tensors, logical masks, complex tensors, string arrays, character arrays, and gpuArray values.
- Character arrays stay 2-D; only
[1 2](identity) and[2 1](transpose) are valid orders. - If the active acceleration provider lacks a dedicated permute kernel, RunMat gathers the data, applies the inverse permutation on the host, and re-uploads so the result remains a gpuArray.
ipermute Function GPU Execution Behaviour
RunMat calls the provider's permute hook with the inverse order vector. Providers that support the
hook perform the reorder entirely on the device. Otherwise RunMat gathers to the host, permutes, and
uploads the result back to the GPU, matching MATLAB's gpuArray behaviour.
Examples of using the ipermute function in MATLAB / RunMat
Reversing a previous permute call
A = reshape(1:24, [2 3 4]);
B = permute(A, [2 1 3]);
C = ipermute(B, [2 1 3]);
isequal(A, C)
Expected output:
ans = logical 1
Restoring matrix orientation after swapping dimensions
M = magic(3);
P = permute(M, [2 1]);
R = ipermute(P, [2 1]);
size(R)
Expected output:
ans = [3 3]
Undoing a permutation with extra singleton dimensions
row = 1:5;
P = permute(row, [2 1 3]);
R = ipermute(P, [2 1 3]);
size(R)
Expected output:
ans = [1 5 1]
Recovering logical masks after reordering axes
mask = false(2, 1, 3);
mask(1, 1, 2) = true;
rot = permute(mask, [3 1 2]);
orig = ipermute(rot, [3 1 2]);
orig(1,1,2)
Expected output:
ans = logical 1
Working with character arrays
chars = ['r','u','n'; 'm','a','t'];
T = permute(chars, [2 1]);
R = ipermute(T, [2 1]);
R
Expected output:
R =
'run'
'mat'
Restoring gpuArray tensors to host layout
G = gpuArray(rand(4, 2, 3));
H = permute(G, [3 1 2]);
R = ipermute(H, [3 1 2]);
isequal(gather(G), gather(R))
Expected output:
ans = logical 1
FAQ
- Do I have to compute the inverse permutation myself?
No. Pass the sameordervector you used withpermute;ipermutecomputes the inverse for you. - What happens if I pass an invalid permutation?
RunMat raises an error when indices repeat, fall outside1:n, or when the order vector is not a row or column vector. - Can
ipermuteintroduce new singleton dimensions?
Yes. Iforderis longer thanndims(A), trailing singleton dimensions are added just like MATLAB. - Does
ipermutemodify the input array in-place?
No. It returns a new array with reordered metadata while leaving the original untouched. - How does
ipermutebehave for character arrays?
Character arrays remain 2-D. Only[1 2]and[2 1]orders are accepted; other permutations raise an error. - Is gpuArray behaviour identical to MATLAB?
Yes. Results remain gpuArray values. When providers lack a device kernel, RunMat gathers, permutes on the host, and uploads the result before returning. - Does
ipermutepreserve data ordering?
Yes. Column-major ordering is preserved. Applyingpermutefollowed byipermutereturns the original array exactly. - Can I use
ipermuteon scalar inputs?
Absolutely. Scalars are treated as 0-D/1-D tensors so the result is the same scalar. - What if the order vector is a gpuArray?
MATLAB requires host numeric vectors for order specifications. RunMat enforces the same rule and raises an error. - Is there a performance benefit on the GPU?
Yes when the provider implements the permute hook. Otherwise the fallback path behaves like MATLAB's gather/permute/gpuArray workflow.
See Also
Source & Feedback
- The full source for
ipermutelives atcrates/runmat-runtime/src/builtins/array/shape/ipermute.rs - Found a behavioural difference? Please open an issue with details and a minimal repro.