Array Functions
MATLAB provides the following functions to sort, rotate, permute, reshape, or shift array contents.
Function | Purpose |
---|---|
length | Length of vector or largest array dimension |
ndims | Number of array dimensions |
numel | Number of array elements |
size | Array dimensions |
iscolumn | Determines whether input is column vector |
isempty | Determines whether array is empty |
ismatrix | Determines whether input is matrix |
isrow | Determines whether input is row vector |
isscalar | Determines whether input is scalar |
isvector | Determines whether input is vector |
blkdiag | Constructs block diagonal matrix from input arguments |
circshift | Shifts array circularly |
ctranspose | Complex conjugate transpose |
diag | Diagonal matrices and diagonals of matrix |
flipdim | Flips array along specified dimension |
fliplr | Flips matrix from left to right |
flipud | Flips matrix up to down |
ipermute | Inverses permute dimensions of N-D array |
permute | Rearranges dimensions of N-D array |
repmat | Replicates and tile array |
reshape | Reshapes array |
rot90 | Rotates matrix 90 degrees |
shiftdim | Shifts dimensions |
issorted | Determines whether set elements are in sorted order |
sort | Sorts array elements in ascending or descending order |
sortrows | Sorts rows in ascending order |
squeeze | Removes singleton dimensions |
transpose | Transpose |
vectorize | Vectorizes 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