Main Content

Matrices and Arrays

Array creation, combining, reshaping, rearranging, and indexing

Matrices and arrays are the fundamental representation of information and data in MATLAB®. You can create common arrays and grids, combine existing arrays, manipulate an array's shape and content, and use indexing to access array elements. For an overview of matrix and array manipulation, watch Working with Arrays.

Functions

expand all

zerosCreate array of all zeros
onesCreate array of all ones
randUniformly distributed random numbers
trueLogical 1 (true)
falseLogical 0 (false)
eyeIdentity matrix
diagCreate diagonal matrix or get diagonal elements of matrix
blkdiagBlock diagonal matrix
catConcatenate arrays
horzcatConcatenate arrays horizontally
vertcatConcatenate arrays vertically
repelemRepeat copies of array elements
repmatRepeat copies of array
combinationsGenerate all element combinations of arrays (Since R2023a)
linspaceGenerate linearly spaced vector
logspaceGenerate logarithmically spaced vector
freqspaceFrequency spacing for frequency response
meshgrid2-D and 3-D grids
ndgridRectangular grid in N-D space
lengthLength of largest array dimension
sizeArray size
ndimsNumber of array dimensions
numelNumber of array elements
isscalarDetermine whether input is scalar
isvectorDetermine whether input is vector
ismatrixDetermine whether input is matrix
isrowDetermine if input is row vector
iscolumnDetermine if input is column vector
isemptyDetermine whether array is empty
issortedDetermine if array is sorted
issortedrowsDetermine if matrix or table rows are sorted
isuniformDetermine if vector is uniformly spaced (Since R2022b)

Resize

headGet top rows of array or table
tailGet bottom rows of array or table
resizeResize data by adding or removing elements (Since R2023b)
paddataPad data by adding elements (Since R2023b)
trimdataTrim data by removing elements (Since R2023b)

Reshape

permutePermute array dimensions
ipermuteInverse permute array dimensions
shiftdimShift array dimensions
reshapeReshape array by rearranging existing elements
squeezeRemove dimensions of length 1

Rearrange

sortSort array elements
sortrowsSort rows of matrix or table
flipFlip order of elements
fliplrFlip array left to right
flipudFlip array up to down
rot90Rotate array 90 degrees
transposeTranspose vector or matrix
ctransposeComplex conjugate transpose
circshiftShift array circularly
colonVector creation, array subscripting, and for-loop iteration
endTerminate block of code or indicate last array index
ind2subConvert linear indices to subscripts
sub2indConvert subscripts to linear indices

Topics