MATLAB - Array Functions



Array functions are a fundamental aspect of MATLAB, as they allow you to perform operations on arrays (also known as matrices) efficiently.

Let us take a look at all these array functions listed below with explanation and examples.

Function Name Description
zeros This function will create an array with all zeros.
ones This function will create an array with all ones.
eye This function helps to create an identity matrix. Identity matrix is a matrix with ones on the main diagonal and zeros for others.
rand This function helps you generate matrices with random numbers distributed uniformly.
randn This function helps you generate matrices of normal distributed random numbers.
size This function will return the size of the matrix as a row vector.For example if the size of the matrix is mxn it will return [m,n].
reshape This function helps to reshape() the given array.
transpose This will give the transpose of the given matrix.
max This will return the maximum element from the array or row vector.
min This will return the minimum element from the array or row vector.
sum This function will give the sum of the array elements.
mean This function will give the mean or average of the given array.
diag This function helps to create a diagonal matrix.
unique This will return the unique elements from the given array or matrix.
sort This function will sort the array elements.

Function Name : zeros()

This function is used to create a matrix or array with all zeros.

Syntax

Z = zeros(n)

Using the function zeros(n) will return the nxn matrix with all zeros.

Example

A = zeros(3)

On execution you will get following output −

A =

     0     0     0
     0     0     0
     0     0     0

You can also specify the size you want , for example zeros(4,2) and the output will be −

ans =

     0     0
     0     0
     0     0
     0     0

Function Name: ones()

You will get an array or matrix with all ones.

Syntax

A = ones(n)
A = ones(sz1szN)

ones(n) will create a matrix of nxn size. ones(sz1sz1n) will create a matrix of size sz1xszn.

Example

A = ones(4)

When you execute the same in matlab command window the output is −

A =

     1     1     1     1
     1     1     1     1
     1     1     1     1
     1     1     1     1

Let us create matrix with ones of dimension 3x2 as shown below −

A = ones(3,2)

When you execute the same in matlab command window the output is −

A =
     1     1
     1     1
     1     1 

Function Name: eye()

You will get an identity matrix with the above function.

Syntax

I = eye(n)
I = eye(n,m)

With eye(n) you will get an nxn matrix and with eye(n,m) you get a matrix of size nxm.

Example

I = eye(4)
I = eye(2, 3)

When you check the output is matlab command window the output is −

I =
 Diagonal Matrix
 
     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1

I =
 Diagonal Matrix
 
     1     0     0
     0     1     0

Function name : rand()

Matrix with random numbers is generated with the above function.

Syntax

rand(n)
rand(sz1szn)

You can create a matrix of random numbers of size n or size sz1xszn.

Example

A = rand(4)
A = rand(5,3)

When you execute them in matlab command window the output is −

A =

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

A =

    0.4218    0.0357    0.7431
    0.9157    0.8491    0.3922
    0.7922    0.9340    0.6555
    0.9595    0.6787    0.1712
    0.6557    0.7577    0.7060

Function name : randn()

Matrices with random numbers that are normally distributed are generated using the above function.

Syntax

randn(n)
randn(sz1szn)

You can create a matrix of random numbers of size n or size sz1xszn.

Example

A = randn(4)
A = randn(5,3)

When you execute them in matlab command window the output is −

A =

   -1.1471    1.4384   -1.7115    0.3129
   -1.0689    0.3252   -0.1022   -0.8649
   -0.8095   -0.7549   -0.2414   -0.0301
   -2.9443    1.3703    0.3192   -0.1649

A =

    0.6277   -1.2141    0.3714
    1.0933   -1.1135   -0.2256
    1.1093   -0.0068    1.1174
   -0.8637    1.5326   -1.0891
    0.0774   -0.7697    0.0326 

Function name: size()

This method returns the size of the array or matrix.

Syntax

size(A)

Here A is a matrix and the function size() will return the dimensions of A in the form of row vector.

Example

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

When you execute the above in the matlab command window the output is −

A =

     1     2     3
     4     5     6
     7     8     9

ans =

     3     3

Function name: reshape()

This function will reshape the array with the size given.

Syntax

B = reshape(A,sz)

Here sz is the size of the vector i.e [3,2] and matrix A will be reshaped to the size given.

Example

A = 1:8
B = reshape(A,[4,2])

When you check the output in matlab the output is −

A =

     1     2     3     4     5     6     7     8

B =

     1     5
     2     6
     3     7
     4     8

Function name: transpose()

This function will return the transpose of the given matrix or vector.

The output will have rows and columns interchanged when the transpose is done.

Syntax

B = transpose(A)

Example

A = magic(3)
B = transpose(A)

The output for above is −

A =

     8     1     6
     3     5     7
     4     9     2

B =

     8     3     4
     1     5     9
     6     7     2 

Function name : max()

This will give the maximum elements of an array as follows.

If the given input is a row vector , the maximum element from the row vector is given.

If the given input is a matrix, the row vector having the maximum element is given.

Syntax

B = max(A)

Here A is an array or matrix or a row vector.

Example 1

A = [23, 84, 36, 1, 20]
B = max(A)

When you execute in matlab command window the output is −

A =

    23    84    36     1    20

B = 84

Now let us take a matrix and find the max element using max().

Example 2

A = [56, 23, 90; 101, 45, 22;78, 11,90]
B = max(A)

When you execute above in matlab command window the output is −

A =

    56    23    90
   101    45    22
    78    11    90

B =

   101    45    90

Function name : min()

This will give the minimum elements of an array as follows.

If the given input is a row vector , the minimum element from the row vector is given.

If the given input is a matrix, the row vector having the minimum element is given.

Syntax

B = min(A)

Here A is an array or matrix or a row vector.

Example 1

A = [23, 84, 36, 1, 20]
B = min(A)

When you execute in matlab command window the output is −

A =

    23    84    36     1    20

B = 1

Now let us take a matrix and find the minimum element using min().

Example 2

A = [56, 23, 90; 101, 45, 22;78, 11,90]
B = min(A)

When you execute above in matlab command window the output is −

A =

    56    23    90
   101    45    22
    78    11    90

B =

    56    11    22

Function name: sum()

This method will return the sum of elements for a row vector or matrix.

If a row vector , it will return the sum of all the elements in the row vector.

If matrix, it will sum of all the columns and return row vectors.

Syntax

S = sum(A)

Here A is an array or matrix or a row vector.

Example 1

S = [2, 3, 80, 90, 12]
sum(S)

When you execute the output is −

S =

   2     3    80    90    12

ans = 187

Example 2

Now let us make use of the matrix as shown below and calculate the sum of it.

A = [1, 2,3;4,5,6;7,8,9]
S = sum(A)

When you execute the output is −

A =

     1     2     3
     4     5     6
     7     8     9

S =

    12    15    18

Function Name : mean()

This function will give the average or mean for the given array.

Syntax

M = mean(A)

If the given input is a row vector, then mean(A) will give you the mean of the elements.

If the given input is a matrix, then mean(A) will return a row vector which will have the mean of each of the columns.

Example 1

A = [23, 90, 33, 78, 11, 56]
M = mean(A)

When you execute the output will be −

A =

    23    90    33    78    11    56

M = 48.5000

Example 2

A = [51, 34 , 33; 12, 90, 32; 8, 13, 67]
M = mean(A)

In above example A is a 3x3 matrix. Now when you find out the mean(A) it will find the mean across each column and return a row vector.

On execution in matlab command window the output is −

A =

    51    34    33
    12    90    32
     8    13    67

M =

   23.6667   45.6667   44.0000

Function name : diag()

This method creates a diagonal matrix for the with the given vector row elements on the main diagonal.

Syntax

D = diag(v)

Here v is a row vector.

Example

v = [5 2 -1 -2 -5]
D = diag(v)

When you execute the same in matlab command window the output is −

v =

     5     2    -1    -2    -5

D =

  Diagonal Matrix

     5     0     0     0     0
     0     2     0     0     0
     0     0    -1     0     0
     0     0     0    -2     0
     0     0     0     0    -5

Function name: unique()

This method will return unique values in an array.

Syntax

C = unique(A)
C = unique(A, setOrder)

You will get an array C in sorted form , with no repetitions of the same values.

The second syntax you have to pass the setOrder, the values for it are : sorted or stable. By default the setOrder is sorted. If you want the final output not to be sorted you can mention it as 'stable'.

Example 1

A = [10, 8, 2, 4,8, 3]
C = unique(A)

When you execute the same in matlab command window the output is −

A =

    10     8     2     4     8     3

C =

     2     3     4     8    10

You will see that the value 8 is appearing twice in array A. When you see the output of unique(A), the repeated values are removed and the final array is in sorted form.

Example 2

Will use the same example we have used above and use the setOrder as stable.

A = [10, 8, 2, 4,8, 3]
C = unique(A, 'stable')

When you execute the same the output is −

A =

    10     8     2     4     8     3

C =

    10     8     2     4     3

Example 3

In case you give the matrix with duplicate values, the unique() method returns the columns vector with unique values as shown below.

A = [1,2,4; 4,2,8; 8,1,5]

C = unique(A)

When you execute the above in matlab command window the output is −

A =

     1     2     4
     4     2     8
     8     1     5

C =

     1
     2
     4
     5
     8

The result C is a single column vector containing the unique values from A.

Function name: sort()

This method will sort the array elements.

Syntax

B = sort(A)
B = sort(A, direction)

The syntax sort(A) will sort the elements of the array in ascending order.

The second syntax sort(A, direction) , here direction takes values ascend and descend.By default it is ascending order.

If the given input A is a vector, then sort(A) will sort the elements in the vector.

If the given input A is a matrix , then sort(A) will sort each column of the matrix.

Example 1

A = [6 -1 -7 2 3 8 -10 11 2]
B = sort(A)

When you execute the same in matlab command window the output is −

A =

     6    -1    -7     2     3     8   -10    11     2

B =

   -10    -7    -1     2     2     3     6     8    11

Example 2

In this example let us take a matrix and use sort on it.

A = [ -1, 7, 10; 3, -9, 2; 8, -2, -3]
B = sort(A)

When you execute the same in matlab command window the output is −

A =

    -1     7    10
     3    -9     2
     8    -2    -3

B =

    -1    -9    -3
     3    -2     2
     8     7    10

In the above example let us use the direction as descend.

A = [ -1, 7, 10; 3, -9, 2; 8, -2, -3]
B = sort(A, 'descend')

When you execute the same in matlab command window the output is −

A =

    -1     7    10
     3    -9     2
     8    -2    -3

B =

     8     7    10
     3    -2     2
    -1    -9    -3
Advertisements