Arrays


>> So far we almost cover all the major things to understan MATLAB. In this section we will learn about arrays. We already discussed vectors and matrices. In MATLAB, all variables of all data types are multidimensional arrays. A vector is a one-dimensional array and a matrix is a two-dimensional array. we will discuss multidimensional arrays. However, before that, let us discuss some special types of arrays.


Special Arrays in MATLAB:


In this section, we will discuss some functions that create some special arrays. For all these functions, a single argument creates a square array, double arguments create rectangular array.


>> zeros() = The zeros() function creates an array of all zeros.


Sometimes we want to initialize a matrix to have all zero elements. The zeros command creates a matrix of all zeros. Typing zeros(n) creates an n × n matrix of zeros, whereas typing zeros(m,n) creates an m × n matrix of zeros. For example:




>> ones() = The ones() function creates an array of all ones.


The syntax of the ones command is the same, except that it creates arrays filled with ones. For example:




>> eye() = The eye() function creates an identity matrix.


The functions eye(n) and eye(size(A)) create an n × n identity matrix and an identity matrix the same size as the matrix A. For example:




>> rand() = The rand() function creates an array of uniformly distributed random numbers on (0,1). For example:




>>  A magic square is a square that produces the same sum, when its elements are added row-wise, column-wise or diagonally.


magic() = The magic() function creates a magic square array. It takes a singular argument that gives the size of the square. The argument must be a scalar greater than or equal to 3. For example:




Multidimensional Arrays:


An array having more than two dimensions is called a multidimensional array in MATLAB. Multidimensional arrays in MATLAB are an extension of the normal two-dimensional matrix. Generally to generate a multidimensional array, we first create a two-dimensional array and extend it. For example, let's create a two-dimensional array a.



The array a is a 3-by-3 array; we can add a third dimension to a, by providing the values like:



We can also create multidimensional arrays using the ones(), zeros() or the rand() functions. For example,




We can also use the cat() function to build multidimensional arrays. It concatenates a list of arrays along a specified dimension:



Syntax for the cat() function is: B = cat(dim, A1, A2...)
Where,

a. B is the new array created.


b. A1, A2, ... are the arrays to be concatenated.



c. dim is the dimension along which to concatenate the arrays.

for example:




Array Functions:


MATLAB provides the following functions to sort, rotate, permute, reshape, or shift array contents.




Examples:


The following examples illustrate some of the functions mentioned above. 


>> Length, Dimension and Number of elements:




>> Circular Shifting the Array Elements:




Sorting Arrays:


This is used to sort the arrays for example,




Cell Array:


Cell arrays are arrays of indexed cells where each cell can store an array of a different dimension and data type.


The cell function is used for creating a cell array. Syntax for the cell function is:


C = cell(dim)

C = cell(dim1,...,dimN)
D = cell(obj)
Where,

a. C is the cell array;


b. dim is a scalar integer or vector of integers that specifies the dimensions of cell array C;


c. dim1, ... , dimN are scalar integers that specify the dimensions of C;


d. obj is One of the following:  >> Java array or object  >> .NET array of type System.String or System.Object


Example:




Accessing Data in Cell Arrays:


There are two ways to refer to the elements of a cell array:


a. Enclosing the indices in first bracket (), to refer to sets of cells


b. Enclosing the indices in braces {}, to refer to the data within individual cells


When you enclose the indices in first bracket, it refers to the set of cells. Cell array indices in smooth parentheses refer to sets of cells. For example:




You can also access the contents of cells by indexing with curly braces. For example: