
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
MATLAB - Array Indexing
Array addressing refers to accessing the elements of an array. Matlab comes with many ways you can access an array. We can make use of indexing, slicing and logical indexing to get the elements from the array.
Array Indexing
A variable is an array in matlab with many numbers in it. To access any element from an array you have to make use of indexing.
To understand in more detail let us create a matrix of size 4x3.
Example
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
On execution in Matlab we get the following output −
A = 1 2 3 4 5 6 7 8 9 10 11 12
The elements from the array can be fetched −
- By giving the row and column subscripts
- By giving the single subscript.
- By using colon operators with start:end range.
By Giving the Row and Column Subscripts
To get the element from the array, give the row number and column number.
For example I want the element 8, which is located in the 3rd row and 2nd column −
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12] A(3,2)
When you execute in matlab you will get −
A = 1 2 3 4 5 6 7 8 9 10 11 12 ans = 8
By Giving the Single Subscript
So from the matrix below you want to fetch the element 5, you can do so by giving a single subscript.
A = 1 2 3 4 5 6 7 8 9 10 11 12
The subscript to fetch 5 is 6.
So A(6) will give you the element 5. Let us check the same in matlab.
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12] A(6)
A = 1 2 3 4 5 6 7 8 9 10 11 12 ans = 5
By Using Colon Operators with start:end Range
The colon is the most commonly used operator in matlab, it is denoted as (:). It is used to get subscript arrays, vectors and also used in for loops.
Here are different results that you will get when you use colon(:) to get elements from a matrix.
Syntax | Description |
---|---|
A(:, nth) | This will display the nth column of the matrix A. |
A(m, : ) | This will display the mth row of the matrix A. |
A(:) | This will give you all elements of the matrix A in a single column vector. |
A(i:j) | This will give you a vector array that will have elements as shown below [A(i), A(i+1) . Aj] |
A(:, i:j) | This will give you all the elements from the first dimension, the second dimension will be based on i:j values. |
Let us check a few examples for a given matrix A.
Example 1
This example will fetch us the third columns from 4x3 matrix as shown below −
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12] A(:, 3)
On execution in matlab the output is −
A = 1 2 3 4 5 6 7 8 9 10 11 12 ans = 3 6 9 12
Example 2
This example will get the 1st row of the matrix A as shown below −
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12] A(1,:)
On execution in Matlab the output is as follows −
A = 1 2 3 4 5 6 7 8 9 10 11 12 ans = 1 2 3
Example 3
Let us display all the elements in the matrix A as a single column vector as shown below.
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12] A(:)
The output on execution is as follows −
A = 1 2 3 4 5 6 7 8 9 10 11 12 ans = 1 4 7 10 2 5 8 11 3 6 9 12
Example 4
Let us get elements from the matrix in order as i, i+1 etc. from the range given. Suppose you want the elements from 2 to 8, A(2:8) will return a row vector as shown below.
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12] A(2:8)
On execution the output is as follows −
A = 1 2 3 4 5 6 7 8 9 10 11 12 ans = 4 7 10 2 5 8 11
Example 5
Let us try another example where we need all the elements from the rows and range of elements from the second dimension.
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12] A(:, 2:3)
On execution the output is as follows −
A = 1 2 3 4 5 6 7 8 9 10 11 12 ans = 2 3 5 6 8 9 11 12