flip
Flip order of elements
Description
B = flip(
returns
array A
)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
.
Examples
Flip Character Vector
Flip Column Vector
Flip Matrix
Create a diagonal matrix, A
.
A = diag([100 200 300])
A = 3×3
100 0 0
0 200 0
0 0 300
Flip A
without specifying the dim
argument.
B = flip(A)
B = 3×3
0 0 300
0 200 0
100 0 0
Now, flip A
along the second dimension.
B = flip(A,2)
B = 3×3
0 0 100
0 200 0
300 0 0
Flip N-D Array
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 = flip(A)
B = B(:,:,1) = 3 2 1 B(:,:,2) = 6 5 4
Now, flip A
along the third dimension.
B = flip(A,3)
B = B(:,:,1) = 4 5 6 B(:,:,2) = 1 2 3
Flip Cell Array
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 = flip(A)
B=3×2 cell array
{'aaa'} {'bbb' }
{[999]} {[ 1]}
{'foo'} {[1000]}
Now, flip A
along the second dimension.
B = flip(A,2)
B=3×2 cell array
{[1000]} {'foo'}
{[ 1]} {[999]}
{'bbb' } {'aaa'}
Input Arguments
A
— Input array
vector | matrix | multidimensional array | table | timetable
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))
dim
— Dimension to operate along
positive integer scalar
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 of size greater than 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
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
Usage notes and limitations:
If specified, the
dim
argument must be greater than1
.
For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Does not support cell arrays for the first argument.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2013b
Ouvrir l'exemple
Vous possédez une version modifiée de cet exemple. Souhaitez-vous ouvrir cet exemple avec vos modifications ?
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)