Is there a builtin way to flip the i-th dimension of an nd-array?

matlab, multidimensional-array

Solution

If you have R2013b+ you should use the new `flip` function:

A = rand(2,2,2);
B = flip(A,3);

For older versions you can use `flipdim`:

A = rand(2,2,2);
B = flipdim(A,3);

Type `edit flipdim` in your command window to see the clever code for `flipdim`.

Problem

Basically, I'm looking for a generalization of `flipud` and `fliplr` to flip the i-th dimension of an nd-array. When the dimension to be flipped is the first one, I guess I can use ``` function flipped = flipfirst(ndarr) sz = size(ndarr); flipped = reshape(flipud(reshape(ndarr, sz(1), [])), sz); end ``` Likewise, if the dimension to be flipped is the last one, I could use ``` function flipped = fliplast(ndarr) sz = size(ndarr); flipped = reshape(fliplr(reshape(ndarr, [], sz(end))), sz); end ``` I'm sure that I can code something more general, with calls to `permute` and whatnot, but is there anything built-in for this? I'm not sure how expensive it is to do all the `reshape`-ing above, but if it is, I'd also be interested in more efficient non-built-in approaches.

Original source

Related problems