File Exchange

## flip

version 1.4.0.0 (1.03 KB) by
Flip vectors and matrices in all directions

2 Downloads

Updated 29 Jul 2014

View License

Flip flips vectors and matrices in all directions
unlike fliplr and flipud functions, FLIP don't preserve row nor columns.
Return x = [ x[N],x[N-1],...,x ] for N length column or row vector;
Return X = [ X[M,N],X[M,N-1],...X[M,1]; ]
[ X[M-1,N],X[M-1,N-1],...X[M-1,1]; ]
... .. ...
[ X[1,N], X[1,N-1], ... X[1,1] ] for M,N matrix;

### Cite As

Marco Borges (2021). flip (https://www.mathworks.com/matlabcentral/fileexchange/47279-flip), MATLAB Central File Exchange. Retrieved .

### Comments and Ratings (6)

Yang Xu

good

Jan

Another interesting point is the "linear indexing": x(end:-1:1) accesses all elements even of multi-dimensional arrays.

Please fix the documentation here and in the code: The vector of length N is:
[x(N), x(N-1), ..., x(1)]. It does not start at x(N-1).

The help section has a H1-line, a useful description of the procedure, and a See Also line. The file contains a change log and the name of the author. The actual code is nearly trivial, so I do not think, that this submission is very useful for others. Therefore I rate it with 4 stars instead of 5, although I do not see any point, where it could be improved.

Marco Borges

then instead of manipulating the indices of the variable to the right of =, you manipulate the indices of the variable that receives value and it does not change the size of the vector! Interesting!!! Thank you so much for your contribution. I'll keep the function any way because it serves for for learning purposes!

Jan

The same command works for matrices and multi-dimensional errors also:

x = [1,2; 3,4];
x(end:-1:1) = x;
disp(x)
>> [4,3; 2,1]

This is much faster and simpler than the FOR-loop. In fact, it is such simple that I'd hesitate to create a function for it.

Marco Borges

of course, I should have thought of that at night I made the code!
I'll update right now! Thanks Jan Simon!

Jan

A vector of length N has the elements from 1 to N, not to N-1.

What about the much easier code, which considers arrays of all dimensions with a single line:

x(end:-1:1) = x;

##### MATLAB Release Compatibility
Created with R2012b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!