Array Functions



Prev TutorialNext Tutorial

MATLAB provides the following functions to sort, rotate, permute, reshape, or shift array contents.
FunctionPurpose
lengthLength of vector or largest array dimension
ndimsNumber of array dimensions
numelNumber of array elements
sizeArray dimensions
iscolumnDetermines whether input is column vector
isemptyDetermines whether array is empty
ismatrixDetermines whether input is matrix
isrowDetermines whether input is row vector
isscalarDetermines whether input is scalar
isvectorDetermines whether input is vector
blkdiagConstructs block diagonal matrix from input arguments
circshiftShifts array circularly
ctransposeComplex conjugate transpose
diagDiagonal matrices and diagonals of matrix
flipdimFlips array along specified dimension
fliplrFlips matrix from left to right
flipudFlips matrix up to down
ipermuteInverses permute dimensions of N-D array
permuteRearranges dimensions of N-D array
repmatReplicates and tile array
reshapeReshapes array
rot90Rotates matrix 90 degrees
shiftdimShifts dimensions
issortedDetermines whether set elements are in sorted order
sortSorts array elements in ascending or descending order
sortrowsSorts rows in ascending order
squeezeRemoves singleton dimensions
transposeTranspose
vectorizeVectorizes expression

Examples

The following examples illustrate some of the functions mentioned above.
Length, Dimension and Number of elements:
Create a script file and type the following code into it −
x = [7.1, 3.4, 7.2, 28/4, 3.6, 17, 9.4, 8.9];
length(x)  % length of x vector
y = rand(3, 4, 5, 2);
ndims(y)    % no of dimensions in array y
s = ['Zara', 'Nuha', 'Shamim', 'Riz', 'Shadab'];
numel(s)   % no of elements in s
When you run the file, it displays the following result −
ans =  8
ans =  4
ans =  23
Circular Shifting of the Array Elements −
Create a script file and type the following code into it −
a = [1 2 3; 4 5 6; 7 8 9]  % the original array a
b = circshift(a,1)         %  circular shift first dimension values down by 1.
c = circshift(a,[1 -1])    % circular shift first dimension values % down by 1 
                           % and second dimension values to the left % by 1.
When you run the file, it displays the following result −
a =
     1     2     3
     4     5     6
     7     8     9

b =
     7     8     9
     1     2     3
     4     5     6

c =
     8     9     7
     2     3     1
     5     6     4

Prev TutorialNext Tutorial