English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MATLAB Arrays

All variables of all data types in MATLAB are multidimensional arrays. A vector is a one-dimensional array, and a matrix is a two-dimensional array.

We have discussed vectors and matrices. In this chapter, we will discuss multidimensional arrays. However, before that, let's discuss some special types of arrays.

Special Arrays in MATLAB

In this section, we will discuss some functions for creating special arrays. For all these functions, one parameter creates a square array, and two parameters create a rectangular array.

zeros()function creates an array full of zeros-

For example-

zeros(5)

MATLAB will execute the above statement and return the following result-

ans =
      0     0     0     0     0
      0     0     0     0     0
      0     0     0     0     0
      0     0     0     0     0
      0     0     0     0     0

ones()function creates a full1array-

For example-

ones(4,3)

MATLAB will execute the above statement and return the following result-

ans =
      1     1     1
      1     1     1
      1     1     1
      1     1     1

eye()function creates a unit matrix.

For example-

eye(4)

MATLAB will execute the above statement and return the following result-

ans =
      1     0     0     0
      0     1     0     0
      0     0     1     0
      0     0     0     1

rand()function in (0,1on the above line to create an array of uniformly distributed random numbers-

For example-

rand(3, 5)

MATLAB will execute the above statement and return the following result-

ans =
   0.8147    0.9134    0.2785    0.9649    0.9572
   0.9058    0.6324    0.5469    0.1576    0.4854
   0.1270    0.0975    0.9575    0.9706    0.8003

magic cube

magic squareis a square that produces the same sum when its elements are added row by row, column by column, or diagonally.

magic()function creates a magic square. It takes a single numeric parameter, which specifies the size of the square. The parameter must be greater than or equal to3scalar.

magic(4)

MATLAB will execute the above statement and return the following result-

ans =
   16     2     3    13
   5    11    10     8
   9     7     6    12
   4    14    15     1

Multidimensional array

Arrays with two or more dimensions in MATLAB are called multidimensional arrays. Multidimensional arrays in MATLAB are an extension of conventional two-dimensional matrices.

通常,to generate multidimensional arrays, we first create a two-dimensional array and then extend it.

For example, let's create a two-dimensional array a.

a = [7 9 5; 6 1 9; 4 3 2]

MATLAB will execute the above statement and return the following result-

a =
   7     9     5
   6     1     9
   4     3     2

arrayais3×3array; we can add the third dimensionone, by providing similar values-

a(:, :, 2)=  [ 1 2 3; 4 5 6; 7 8 9]

MATLAB will execute the above statement and return the following result-

a =
ans(:,:,1)  =
   0   0   0
   0   0   0
   0   0   0
ans(:,:,2)  =
   1   2   3
   4   5   6
   7   8   9

We can also create a multidimensional array using the ones(), zeros(), or rand() functions.

For example,

b  =  rand(4,3,2)

MATLAB will execute the above statement and return the following result-

b(:,:,1)  =
   0.0344    0.7952    0.6463
   0.4387    0.1869    0.7094
   0.3816    0.4898    0.7547
   0.7655    0.4456    0.2760
b(:,:,2)  =
   0.6797    0.4984    0.2238
   0.6551    0.9597    0.7513
   0.1626    0.3404    0.2551
   0.1190    0.5853    0.5060

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

The syntax of the cat() function is-

B  =  cat(dim,  A1,  A2...)

where

  • Bis the new array created

  • A1,A2,...is the array to be concatenated

  • dimis the dimension used to connect arrays

Instance

Create a script file and enter the following code-

a = [9 8 7; 6 5 4; 3 2 1];
b  =  [1 2 3; 4 5 6; 7 8 9];
c  =  cat(3,  a,  b,  [ 2 3 1; 4 7 8; 3 9 0])

When the file is run, it displays-

c(:,:,1)  =
      9     8     7
      6     5     4
      3     2     1
c(:,:,2)  =
      1     2     3
      4     5     6
      7     8     9
c(:,:,3)  =
      2     3     1
      4     7     8
      3     9     0

Array function

MATLAB provides the following functions to sort, rotate, transpose, reshape, or shift the content of arrays.

FunctionFunction
length

Vector length or maximum array dimension

ndimsArray dimension
numelNumber of array elements
sizeArray dimension
iscolumn

Determine if input is a column vector

isemptyDetermine if array is empty
ismatrix

Determine if input is a matrix

isrowDetermine if input is a row vector
isscalarDetermine if input is a scalar
isvectorDetermine if input is a vector
blkdiag

Construct block diagonal matrix according to input parameters

circshiftCircularly shift array
ctransposeComplex conjugate transpose
diagDiagonal matrix and matrix diagonal
flipdim

Flip array along the specified dimension

fliplrFlip matrix from left to right
flipudUp-down flip matrix
ipermute

Invert N-Dimension of D array

permute

Reorder N-Dimension of D array

repmatCopy and slice array
reshapeReshape array
rot90Rotate the matrix90 degrees
shiftdimMove dimension
issortedDetermine whether the elements of a set are sorted in order
sort

Sort array elements in ascending or descending order

sortrowsSort rows in ascending order
squeezeRemove singleton dimension
transposeTranspose
vectorizeVectorized expression

Instance

The following examples illustrate some of the functions mentioned above.

Element length, dimension, and quantity-

Create a script file and enter the following code-

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 the file is run, it displays the following result-

ans =  8
ans =  4
ans =  23

Circular shifting of array elements

Create a script file and enter the following code-

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 the file is run, 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

Sorting array

Create a script file and enter the following code-

v = [ 23 45 12 9 5 0 19 17]  % horizontal vector
sort(v)  % sorting v
m = [2 6 4; 5 3 9; 2 0 1]  % two dimensional array
sort(m, 1)  % sorting m along the row
sort(m, 2)  % sorting m along the column

When the file is run, it displays the following result-

v =
   23    45    12     9     5     0    19    17
ans =
   0     5     9    12    17    19    23    45
m =
   2     6     4
   5     3     9
   2     0     1
ans =
   2     0     1
   2     3     4
   5     6     9
ans =
   2     4     6
   3     5     9
   0     1     2

Cell array

A cell array is an array of indexed cells, where each cell can store an array of different dimensions and data types.

cellThe function is used to create an array of cells. The syntax of the cell function is-

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

Description

  • CIs a cell array;

  • dimIs a scalar integer or integer vector used to specify the dimensions of the cell array C;

  • dim1,... ,dimNIs a scalar integer of specified C dimension;

  • objIs one of the following-

    • Java array or object

    • Array of .NET types System.String or System.Object

Instance

Create a script file and enter the following code-

c = cell(2, 5);
c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5}

When the file is run, it displays the following result-

c = 
{
   [1,1] = Red
   [2,1] =  1
   [1,2] = Blue
   [2,2] =  2
   [1,3] = Green
   [2,3] =  3
   [1,4] = Yellow
   [2,4] =  4
   [1,5] = White
   [2,5] =  5
}

Accessing data in a cell array

There are two ways to reference elements of a cell array-

  • Enclose the index in the first square bracket () to refer to the cell set

  • Enclose the index in curly braces {} to refer to the data in a single cell

When you enclose the index in the first parenthesis, it refers to the set of cells.

The index in the parentheses refers to the cell array set.

For example-

c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5};
c(1:2,1:2)

MATLAB will execute the above statement and return the following result-

ans = 
{
   [1,1] = Red
   [2,1] =  1
   [1,2] = Blue
   [2,2] =  2
}

You can also use curly bracket indexing to access the content of cells.

For example-

c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5};
c{1, 2:4}

MATLAB will execute the above statement and return the following result-

ans = Blue
ans = Green
ans = Yellow