MATLAB’s Array

What is an Array?

An array is a collection of similar datatype elements in contiguous memory location. Every element in array is stored with an index number starting from 0. First element of an array is stored in 0th position index. Any element can be easily accessed using the index number.

MATLAB’s Array

In MATLAB, arrays are used to store numbers and provide various function like ones(), zeros() to ease out difficult operations while programming. Multidimensional arrays are supported in MATLAB.

Simple example:

M1=[1,6,2]

N1=[1,2,3;4,5,6;7,2,4]

M1 is one-dimensional array, whereas n1 is multidimensional array.

MATLAB’s  Array

Various operations of arrays

1.ones()

In other programming languages we create array of all element of ones manually but in MATLAB we have been provided with an in-built function to ease out the task.

In MATLAB, to create an array of all elements of ones, we use ones() function.

Example

 >>ones(4)
 OUTPUT:
 Ans =
             1  1  1  1
             1  1  1  1
             1  1  1  1
             1  1  1  1 

2.zeros

In MATLAB, to create an array of all elements of zeros, we use zeros() function.

Example

 >>zeros(4)
 OUTPUT:
 Ans =
             0  0  0  0
             0  0  0  0
             0  0  0  0
             0  0  0  0 

3. rand()

This function is used to create random numbers in an array.

In MATLAB, to create an array of random values, we use rand() function.

Example

 >>rand(2,2)
 OUTPUT:
 Ans =
             0.456   0.789
             0.745   0.457 

4.magic()

This function creates a magic square array. In magic square, when we add elements of row-wise, or column-wise or diagonally we get the same sum.

magic(4)

Output:

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

Multidimensional arrays

In MATLAB multidimensional arrays are supported. It is an extension of our normal arrays. We first try to create a 2D array then we extend it to create multidimensional array. We can use functions which we have earlier discussed to create multidimensional arrays.

Let’s take various examples of Multidimensional Array.

 c = rand(4,4,3)
 
c(:,:,1) =

    0.7572    0.0759    0.9340    0.0119
    0.7537    0.0540    0.1299    0.3371
    0.3804    0.5308    0.5688    0.1622
    0.5678    0.7792    0.4694    0.7943


c(:,:,2) =

    0.3112    0.2630    0.4505    0.1524
    0.5285    0.6541    0.0838    0.8258
    0.1656    0.6892    0.2290    0.5383
    0.6020    0.7482    0.9133    0.9961
c(:,:,3) =

    0.0782    0.0046    0.0844    0.4314
    0.4427    0.7749    0.3998    0.9106
    0.1067    0.8173    0.2599    0.1818
    0.9619    0.8687    0.8001    0.2638 

We have used MATLAB online to execute this code and obtain the result as we can in this screenshot.

MATLAB’s  Array

MatlabStruct to Array Conversions

MATLAB’s  Array

As we can see the above illustration, where cell array of type structs can be transformed to array and cell array irrespective of data it contains and vice versa. Struct array also can be transformed to various forms of array representation like shown above and interchangeably from array, cell array to struct array. Array can be of any dimension can be used to perform the above transformation.

Array functions

Function nameDescriptions
Repmatreplicates and tile array
Reshapearray reshaped
rot90rotates matrix 90 degrees
ShiftdimShifts dimensions
Issortedwhether set elements are in sorted order is checked.
Sortarray elements are sorted in ascending or descending order
Sortrowsrows are sorted in ascending order
Squeezesingleton dimensions is removed.
TransposeTranspose
VectorizeVectorizes expression
LengthLength of largest array dimension
Ndimsarray dimensions
Numelarray elements
Sizearray dimensions
iscolumnwhether input is column vector is checked.
isemptywhether array is empty is checked.
ismatrixwhether input is matrix is checked.
isrowwhether input is row vector is checked.
isscalarwhether input is scalar is checked.
isvectorwhether input is vector is checked.
blkdiagblock diagonal matrix from input arguments is created.
circshiftcircular shift of array is made.
ctransposeconjugate transpose
diagdiagonals of matrix
flipdimFlips array
fliplrFrom left to right array is flipped.
flipudmatrix up to down is flipped
Ipermutepermute dimensions of N-D array is inversed.
Permutedimensions of N-D array is rearranged.

Above-mentioned comments are executed in the MATLAB’s command line with the help of examples matrices.

àPermute() function is executed below.

Create a 3x4x2 array and permute it so that dimensions 1st and 3rd are switched, resulting in a 2x4x3 array.

 rng default
 A = rand(4,3,2)
 B = permute(A,[3 2 1])
 
A(:,:,1) =

    0.8147    0.6324    0.9575
    0.9058    0.0975    0.9649
    0.1270    0.2785    0.1576
    0.9134    0.5469    0.9706


A(:,:,2) =

    0.9572    0.4218    0.6557
    0.4854    0.9157    0.0357
    0.8003    0.7922    0.8491
    0.1419    0.9595    0.9340


B(:,:,1) =

    0.8147    0.6324    0.9575
    0.9572    0.4218    0.6557


B(:,:,2) =

    0.9058    0.0975    0.9649
    0.4854    0.9157    0.0357


B(:,:,3) =

    0.1270    0.2785    0.1576
    0.8003    0.7922    0.8491


B(:,:,4) =

    0.9134    0.5469    0.9706
    0.1419    0.9595    0.9340
 àsort() function is executed with the help of an exampleon Matlab’s command line.
 Script file:
 p = [ 93 85 12 19 5 10 19 17]
 sort(p)                  
 o = [12 16 4; 65 8 9; 2 10 1]
 sort(o, 1)                 
 sort(o, 2) 

Output: We have used MATLAB online to execute this code and obtain the result as we can in this screenshot.

MATLAB’s  Array

àCirshift() function is executed with the help of examples on Matlab’s command line.

 p = [21 21 13; 14 15 6; 27 18 9] 
 s = circshift(a,1)        
 u = circshift(a,[1 -1])
 
p =

    21    21    13
    14    15     6
    27    18     9 
MATLAB’s  Array

Few more functions like length(), ndims() are executed with the help of examples.

 p=[8.1,9.4,1.2,8/4,0.6,7,9,79];
 length(p)
 s=rand(0,7,1,2);
 ndims(s)
 s =['peba','Niha','Shaman','Riya','Shadab'];
 numel(s) 

Output: We have used MATLAB online to execute this code and obtain the result as we can in this screenshot.

MATLAB’s  Array

àndgrid() function is executed with the help of example.

This function replicates the grid vectors y1,y2,...,yn to produce an n-dimensional full grid as shown below.

 [X,Y] = ndgrid(1:3:18,2:2:14)
 
X =

     1     1     1     1     1     1     1
     4     4     4     4     4     4     4
     7     7     7     7     7     7     7
    10    10    10    10    10    10    10
    13    13    13    13    13    13    13
    16    16    16    16    16    16    16


Y =

     2     4     6     8    10    12    14
     2     4     6     8    10    12    14
     2     4     6     8    10    12    14
     2     4     6     8    10    12    14
     2     4     6     8    10    12    14
     2     4     6     8    10    12    14 
MATLAB’s  Array

àflipud() function is executed with the help of example.

 A = {'e' 'b' 'i'; 'e' 'j' 'f'; 'a' 'h' 'i'}
 B = flipud(A) 

Output:

MATLAB’s  Array

Cell Arrays

Cell arrays are used when we want to store different datatypes in one array, where in one cell of cell array, different dimension arrays are stored. We use a function called cell() to create cell arrays in Matlab.

Example:

 o=cell(2,5);
 o={'R','B','G','Y','W';12345} 

output:

 o =
 {
    [1,1] = R
    [2,1] =  1
    [1,2] = B
    [2,2] =  2
    [1,3] = G
    [2,3] =  3
    [1,4] = Y
   [2,4] =  4
    [1,5] = W
    [2,5] =  5
 } 

We have used MATLAB online to execute this code and obtain the result as we can in this screenshot.

MATLAB’s  Array

Conclusion:

Utilizing arrays in different programming language has made the manual undertakings simple and are utilized by the software engineers. There are benefits of utilizing array like getting to the components in the array is simple which can be executed utilizing basic steps, there are no presentation issues in the program, appropriate utilization of memory, quicker execution and accommodating in carrying out the projects utilizing stack trees, linked list, and so on.



ADVERTISEMENT
ADVERTISEMENT