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.


Related Topics

Matlab DFT and IDFT

Matlab Program- (DFT and IDFT of a sequence) Program for DFT (Discrete Fourier change) and IDFT (Inverse discrete Fourier change) of a sequence. Program explanation: In Arithmetic, the discrete Fourier change (DFT) changes...

3 minutes read.

MATLAB Operators

What is an Operator? A specific operation is performed by using a symbol which is called an operator. Example: A+B, where A, B are called operand and '+' is called operator. If you...

8 minutes read.

MATLAB Simulink examples

We will discuss few examples to understand different use-cases of MATLAB’s Simulink. Simulink is basically MATLAB-based. Its essential interface is a graphical square charting apparatus and an adaptable arrangement of square...

4 minutes read.

MATLAB Features

MATLAB Features MATLAB (abbreviation for matrix laboratory) is an interactive programming environment that eases high-power computations in the IT world. Initially, it was developed with the intention of matrix calculation only....

4 minutes read.

MATLAB Setup And Environment

MATLAB Setup And Environment MATLAB System Requirements As we have already discussed the prerequisite to begin the learning journey of MATLAB now let us drive into some technical system requirements. RAMàMinimum 4GB or...

3 minutes read.

MATLAB Loops

Loops in programming Concept of looping is used to execute instructions repeatedly. It helps coders to reduce lines of code by setting a condition. In looping first a condition is fixed,...

3 minutes read.

MATLAB Tutorial

Introduction of MATLAB In this MATLAB(stands for matrix laboratory) tutorial, we will cover core concepts of MATLAB and provide a solid foundation to enhance high computing skills. We are going to...

4 minutes read.

Matlab Square Wave Signal Generation

What is Square Wave signal Generator? A “square wave” is a sort of non-sinusoidal waveform, most normally experienced in gadgets and sign preparing. An optimal square wave substitutes consistently and momentarily...

3 minutes read.

MATLAB Control Statements

What are control statements? The name itself suggests that something is being controlled, these control statements decide or alter the flow of execution ina program. In program, we often need to...

3 minutes read.

MATLAB special graphic function

In this section, we portray a greater amount of MATLAB's illustrations graphic functions and the most well-known methods of controlling and redoing graphics. Let’s type help designs, help graph2d (for...

7 minutes read.

MATLAB vs Other Languages

MATLAB vs Other Languages If we try to find which is the best computer programming languages, the answer is not straightforward, and that might sound similar to asking which is the...

3 minutes read.

MATLAB Scripts

Scripts Introduction Matlab Script represents some programs and is executed like we execute command in the command window of Matlab’s environment. Matlab script is also defined as a sequence of commands;...

6 minutes read.

matlab Integral

What is integration? In maths, Integral allots numbers to functions in a manner that portrays relocation, region, volume, and different ideas that emerge by consolidating tiny information. The way toward discovering...

5 minutes read.

MATLAB Gaussian Pulse

Gaussian pulse shape description: The diagram of a Gaussian is a trademark symmetric "ringer bend" shape. The boundary an is the stature of the bend's pinnacle, b is the situation of...

3 minutes read.

Matlab Calculus

Analytics Calculus is significant in various fields like designing, physical science, and different sciences, yet the estimations are finished by machines or PCs, regardless of whether they are into Physics...

3 minutes read.

Matlab function

Introduction: Functions in MATLAB are composed with code that link one variable with another, and yielded output is connected precisely to one specific information that shapes a significant piece of any...

6 minutes read.

MATLAB Program - To Solve Differential Equation Using Euler’s Method

Using Euler's method to solve differential equation Can you imagine a point where we do not use differential equation to solve differential condition? Truth be told, there are a few different...

3 minutes read.

Matlab Algebra

Up until now, we have seen that every one of the models work in MATLAB just as its GNU, then again called Octave. In any case, for addressing fundamental mathematical...

6 minutes read.

Matlab Simulink

What is Simulink? Simulink is a re-enactment and model-based plan climate for dynamic and installed frameworks, incorporated with MATLAB. Simulink, additionally created by "MathWorks", is an information stream graphical programming language apparatus...

7 minutes read.

MATLAB Commands

MATLAB Commands MATLAB (stands for matrix laboratory) is a high-level programming environment for scientific calculation, and it provides functions more efficiently than other programming languages. In addition, it has many in-build...

3 minutes read.