flip - Flip order of elements - MATLAB (original) (raw)
Syntax
Description
B = flip([A](#btzz7d7-1-A))
returns array B
the same size as A
, but with the order of the elements reversed. The dimension that is reordered in B
depends on the shape of A
:
- If
A
is vector, thenflip(A)
reverses the order of the elements along the length of the vector. - If
A
is a matrix, thenflip(A)
reverses the elements in each column. - If
A
is an N-D array, thenflip(A)
operates on the first dimension ofA
in which the size value is not1
.
B = flip([A](#btzz7d7-1-A),[dim](#btzz7d7-1-dim))
reverses the order of the elements in A
along dimension dim
. For example, if A
is a matrix, then flip(A,1)
reverses the elements in each column, and flip(A,2)
reverses the elements in each row.
Examples
A = 'no word, no bond, row on.'; B = flip(A)
B = '.no wor ,dnob on ,drow on'
Create a diagonal matrix, A
.
A = 3×3
100 0 0 0 200 0 0 0 300
Flip A
without specifying the dim
argument.
B = 3×3
0 0 300
0 200 0
100 0 0
Now, flip A
along the second dimension.
B = 3×3
0 0 100
0 200 0
300 0 0
Create a 1-by-3-by-2 array.
A = zeros(1,3,2); A(:,:,1) = [1 2 3]; A(:,:,2) = [4 5 6]; A
A = A(:,:,1) =
1 2 3
A(:,:,2) =
4 5 6
Flip A
without specifying the dim
argument.
B = B(:,:,1) =
3 2 1
B(:,:,2) =
6 5 4
Now, flip A
along the third dimension.
B = B(:,:,1) =
4 5 6
B(:,:,2) =
1 2 3
Create a 3-by-2 cell array.
A = {'foo',1000; 999,true; 'aaa','bbb'}
A=3×2 cell array {'foo'} {[1000]} {[999]} {[ 1]} {'aaa'} {'bbb' }
Flip A
without specifying the dim
argument.
B=3×2 cell array {'aaa'} {'bbb' } {[999]} {[ 1]} {'foo'} {[1000]}
Now, flip A
along the second dimension.
B=3×2 cell array {[1000]} {'foo'} {[ 1]} {[999]} {'bbb' } {'aaa'}
Input Arguments
Input array, specified as a vector, matrix, multidimensional array, table, or timetable.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| struct
| cell
| table
| timetable
| categorical
| datetime
| duration
| calendarDuration
Example: [1 2 3 4]
Example: ['abcde']
Example: [1 2; 3 4]
Example: {'abcde',[1 2 3]}
Example: table(rand(1,5),rand(1,5))
Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension whose size does not equal 1.
Consider an m
-by-n
input matrix, A
:
flip(A,1)
reverses the order of the elements in each column ofA
and returns anm
-by-n
matrix.flip(A,2)
reverses the order of the elements in each row ofA
and returns anm
-by-n
matrix.
Extended Capabilities
Theflip
function supports tall arrays with the following usage notes and limitations:
- If specified, the
dim
argument must be greater than1
.
For more information, see Tall Arrays.
Usage notes and limitations:
- Does not support cell arrays for the first argument.
The flip
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2013b