EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Matlab Tutorial MATLAB Colon
Secondary Sidebar
Matlab Tutorial
  • Functions
    • MATLAB Functions
    • MATLAB user defined function
    • Calling Functions in MATLAB
    • Transfer Functions in MATLAB
    • Anonymous Functions in MATLAB
    • Inline Functions in MATLAB
    • Bessel Functions in MATLAB
    • Mean Function in MATLAB
    • Find Function MATLAB
    • Filter Function in MATLAB
    • IIR Filter MATLAB
    • Piecewise Function in MATLAB
    • Sum Function in MATLAB
    • Simulink MATLAB Function
    • MATLAB Create Function
    • MATLAB Inverse Function
    • MATLAB Count
    • Step Function MATLAB
    • MATLAB limit
    • Fminsearch in MATLAB
    • Covariance in MATLAB
    • Summation in MATLAB
    • Linear Fit MATLAB
    • MATLAB?linear regression
    • MATLAB Derivative
    • MATLAB Derivative of Function
    • MATLAB Comet()
    • Fzero MATLAB
    • xlabel MATLAB
    • Matplotlib Legend
    • Matplotlib Subplots
    • Plot graph MATLAB
    • MATLAB Format
    • MATLAB plot title
    • Multiple Plots in MATLAB
    • MATLAB Indexing
    • Ceil MATLAB
    • Curve Fitting MATLAB
    • MATLAB trapz()
    • MATLAB Normalize
    • MATLAB diff
    • MATLAB sym()
    • MATLAB Syms
    • Absolute Value MATLAB
    • MATLAB Exponential
    • Kalman Filter MATLAB
    • Low Pass Filter MATLAB
    • Bandpass Filter MATLAB
    • MATLAB Unique
    • Trapezoidal Rule MATLAB
    • MATLAB Root Finding
    • MATLAB stem()
    • MATLAB loglog()
    • MATLAB Autocorrelation
    • MATLAB Sort
    • Simplify MATLAB
    • Cumsum MATLAB
    • Eval Function MATLAB
    • Polyval MATLAB
    • MATLAB Colon
    • MATLAB Eigenvalues
    • MATLAB fit
    • Delta Function MATLAB
    • MATLAB Remainder
    • Differentiation in MATLAB
    • Permute MATLAB
    • isempty MATLAB
    • MATLAB text()
    • MATLAB Display Text
    • Varargin in MATLAB
    • MATLAB gca
    • MATLAB fill()
    • MATLAB pcolor()
    • MATLAB min
    • MATLAB xcorr
    • MATLAB? color codes
    • Semilogy MATLAB
    • MATLAB? eye
    • feval MATLAB
    • num2str in MATLAB
    • MATLAB Images
    • MATLAB Image? Segmentation
    • Imagesc MATLAB
    • MATLAB Image Processing
    • MATLAB Image Resize
    • MATLAB Flag
    • MATLAB fopen
    • Strcmp MATLAB
    • MATLAB fwrite
    • MATLAB fft()
    • MATLAB zeros()
    • MATLAB textread
    • Arctan MATLAB
    • MATLAB Scripts
    • Butterworth filter MATLAB
    • MATLAB Findpeaks
    • MATLAB find Index
    • MATLAB Cell
    • MATLAB Unit Step Function
    • MATLAB Backslash
    • MATLAB Mod
    • Size Function in MATLAB
    • Secant MATLAB
  • Basic
    • MATLAB Area Under Curve
    • MATLAB not equal
    • MATLAB max
    • MATLAB exist
    • MATLAB Table
    • MATLAB regression
    • MATLAB Lists
    • MATLAB quantile
    • MATLAB Round
    • MATLAB readtable
    • MATLAB disp
    • MATLAB Standard Deviation
    • MATLAB quadprog
    • MATLAB Transpose
    • Introduction to MATLAB
    • Advantages of MATLAB
    • MATLAB Features
    • Taylor Series MATLAB
    • MATLAB Z Transform
    • fsolve in MATLAB
    • MATLAB QR
    • Career in MATLAB
    • Uses Of MATLAB
    • MATLAB Free
    • How to Install MATLAB
    • How to Use MATLAB?
    • MATLAB Version
    • MATLAB Compiler
    • MATLAB Commands
    • MATLAB Block Comment
    • MATLAB? sprintf
    • MATLAB fprintf
    • Data Types in MATLAB
    • MATLAB Integral
    • MATLAB Double Integral
    • MATLAB boolean
    • MATLAB vpa
    • MATLAB Object
    • MATLAB Annotation
    • MATLAB Variables
    • MATLAB Global Variables
    • MATLAB Operators
    • MATLAB Logical Operators
    • MATLAB nan
    • MATLAB Patch
    • MATLAB AND Operator
    • MATLAB OR Operator
    • Vectors in MATLAB
    • What is Simulink in MATLAB
    • MATLAB Interpolation
    • MATLAB Imread
    • fscanf MATLAB
    • Euler Method MATLAB
    • Root Locus MATLAB
    • MATLAB return
    • Bode Plot MATLAB
    • Nargin MATLAB
    • MATLAB Matrix Inverse
    • MATLAB String to Number
    • MATLAB string
    • MATLAB ColorBar
    • MATLAB Surfc
    • MATLAB Concatenate
    • NUMEL MATLAB
    • MATLAB? File Extension
    • MATLAB File
    • MATLAB Smooth
    • MATLAB ones
    • Exponential in MATLAB
    • MATLAB ksdensity
    • MATLAB log
    • MATLAB Append
    • MATLAB hold on
    • MATLAB Magnitude of Vector
    • Heatmap in MATLAB
    • MATLAB xticks
    • MATLAB randn
  • Control Statements
    • IF-Else Statement in MATLAB
    • If Statement in MATLAB
    • Loops in MATLAB
    • For Loop in MATLAB
    • While Loop in MATLAB
    • do while loop in MATLAB
    • Nested Loop in MATLAB
    • Switch Statement in MATLAB
    • Break in MATLAB
  • Matrix
    • Matrix in MATLAB
    • 3D Matrix in MATLAB
    • Transpose Matrix MATLAB
    • Sparse Matrix in MATLAB
    • Matrix Multiplication in MATLAB
    • Identity Matrix in MATLAB
    • MATLAB?writematrix
  • Advanced
    • MATLAB Class
    • Arrays in MATLAB
    • Matlab find value in array
    • MATLAB Grader
    • Power Spectral Density MATLAB
    • Matlab Textscan
    • String Array in MATLAB
    • MATLAB Random Numbers
    • Matlab Dot
    • MATLAB 2D Array
    • MATLAB? zero padding
    • MATLAB sort matrix
    • MATLAB Plot Function
    • 2D Plots in MATLAB
    • 3D Plots in MATLAB
    • MATLAB Fread
    • Spectrogram MATLAB
    • MATLAB Average
    • MATLAB exponent
    • MATLAB not enough input arguments
    • MATLAB comment
    • MATLAB zpk
    • Scatter Plots in MATLAB
    • MATLAB 3d scatter plot
    • Bar Graph in MATLAB
    • Bar Plot MATLAB
    • Log Plot MATLAB
    • Polar Plot in MATLAB
    • Surface Plot in MATLAB
    • MATLAB Plot Circle
    • Boxplot in MATLAB
    • MATLAB Plot Multiple Lines
    • Linspace MATLAB
    • Histogram in MATLAB
    • Plot Vector MATLAB
    • MATLAB Legend
    • MATLAB Plot Legend
    • MATLAB ezplots
    • Column Vector MATLAB
    • MATLAB Plot Marker
    • MATLAB LineWidth
    • MATLAB Line Style
    • Contour plot in MATLAB
    • MATLAB Sine Wave
    • Reshape in MATLAB
    • Natural Log in MATLAB
    • Random Number Generator in MATLAB
    • Complex Numbers in MATLAB
    • MATLAB Figure
    • Heatmap in MATLAB
    • MATLAB Technical Computing
    • Colors in MATLAB
    • Colormap in MATLAB
    • MATLAB Plot Colors
    • MATLAB fplot()
    • MATLAB Stacked Bar
    • MATLAB sphere()
    • MATLAB cylinder()
    • MATLAB mesh()
    • Pie Chart in MATLAB
    • MATLAB Gradient
    • Grid on MATLAB
    • Repmat in MATLAB
    • dlmread in MATLAB
    • Meshgrid in MATLAB
    • MATLAB Struct
    • MATLAB Cross Product
    • MATLAB colorbar Label
    • MATLAB Save Variable
    • MATLAB Saveas
    • MATLAB Cell Array
    • Polynomial in MATLAB
    • ismember MATLAB
    • Heaviside MATLAB
    • MATLAB rref
    • MATLAB polyfit()
    • MATLAB xlim
    • MATLAB Variance
    • Optimset MATLAB
    • Quiver MATLAB
    • Newton Raphson MATLAB
    • Mat2cell MATLAB
    • Magnitude MATLAB
    • format long MATLAB
    • Dot Product MATLAB
    • Jacobian MATLAB
    • What is Matlab?
    • Convolution MATLAB
    • Moving Average MATLAB
    • Fourier Series MATLAB
    • Gaussian Fit MATLAB
    • Bisection Method MATLAB
    • Laplace Transform MATLAB
    • Fourier Transform MATLAB
    • Signal Processing MATLAB
    • MATLAB Forms
    • Complex Conjugate MATLAB
    • MATLAB Write to File
    • uigetfile MATLAB
    • MATLAB Toolbox
    • MATLAB Errorbar
    • MATLAB Index Exceeds Matrix Dimensions
    • Nyquist MATLAB
    • Impulse Response MATLAB
    • xlsread MATLAB
    • MATLAB xlswrite
    • Matplotlib Scatter
    • MATLAB Import Data
    • MATLAB Export Data
    • MATLAB Read CSV
  • Programs
    • Square Root in MATLAB
    • Square Wave MATLAB
    • Squeeze MATLAB
    • Factorial in MATLAB
    • Cell to String MATLAB
  • Interview Questions
    • MATLAB Interview Questions

Related Courses

MATLAB Certification Course

R Programming Course

All in One Data Science Courses

MATLAB Colon

By Priya PedamkarPriya Pedamkar

MATLAB Colon

Introduction to MATLAB Colon

‘Colon’ is used as an operator in MATLAB programming and is one of the frequently used operators. This operator comes into the picture in order to create vectors defining with simple expressions, specifying‘for’ iterations or subscribing arrays, or getting access to a set of elements of an existing vector in a sequence. When a vector is created to index into a cell or structure type array using the colon operator in the form of ‘cellName{:}’ or ‘structName(:).fieldName)’, MATLAB results in multiple outputs putting in a comma-separated list.

Syntax

Various forms of syntax are supported to use the operator ‘:’. The functionalities of different syntaxes are described as below:

Syntax

Description

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

li = j:k This syntax is used to create aunit spaced vector list i.e. values with increment value ‘1’, consisting of the elements as [j,j+1,…,k].
li= j:i:k This syntax is used to create a regularly-spaced vector list ‘li’ using values with increment value ‘i’, consisting of the elements [j,j+1,…,k].
M(:,n) This syntax is used to store the nth column of matrix M.
M(m,:) This syntax is used to store the mthrow of matrix M.
M(:) This syntax can be used to reshape the element ‘M’ into a vector containing a single column.
M(j:k) This syntax can be used to apply the vector list having the elementsj: kin order to index into matrix M.

This is equivalent to forming vector as [M(j), M(j+1), …, M(k)].

M(:,:,p) This syntax can be used to store/extract the data from a three-dimensional array A set in the pth page.
M(:,:) This syntax can be used to reshapethe elements of matrix ‘M’ into a matrix of two-dimensional.
M(:,j:k) This syntax can be used to include the subscripts present in the first dimension and to use the vector having elements j:k, for indexing the second dimension. This results in a matrix having columns as [M(:,j), M(:,j+1), …, M(:,k)].

Examples to Implement MATLAB Colon

Below are some examples mentioned:

Example #1

Code:

list_name = -3:3

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,527 ratings)

Output:

Matlab colon1

Explanation: The command has generated a list of values from -3 to 3 having different between 2 consecutive elements as ‘1’.

Example #2

Code:

list_name = -3:3:30

Output:

Matlab colon2

Explanation: The command has generated list of values from -3 to 30 having different between 2 consecutive elements as ‘3’.

Example #3

Code:

Mat_A = magic(3)Mat_A(2,:)

Output:

Matlab colon3

Explanation: The command has displayed the elements from the second row of the matrix ‘Mat_A’

Example #4

Code:

Mat_A = magic(3)Mat_A(:,2)

Output:

Matlab colon4

Explanation: The command has displayed the elements from the second column of the matrix ‘Mat_A’

Example #5

Code:

Mat_A = magic(3)Mat_A(:)

Output:

Matlab colon5

Explanation: The command has displayed the elements of the matrix ‘Mat_A’ in a single column.

Example #6

Code:

Mat_A = magic(4)Mat_A(2:4)

Output:

Matlab colon6

Explanation: The command has displayed the elements of the matrix ‘Mat_A’ indexed between 2 to 4.

Application of MATLAB Colon

Below are the applications:

1. Using colon to create a list

A vector with evenly-spaced numbers can be generated using a colon operator.

Code:

Mat_A =1:2:10

Output:

Using colon to create a list

2. Creating a vector with only column format

The colon operator can be used to transform the input of the row vector type to that of the column vector type. The colon operator can also be used with reshaping or permute functions to create a column vector.

Code:

M = rand(3,2,6,4);
M32sum = sum(M(3,2,:))
M32sumAll = sum(M(3,2,:,:))
%Creating column vector using reshape method
M32vecR = reshape(M32sumAll,[],1)
%Creating column vector using %permute method
M32vecP = permute(M32sumAll, [4 1:3])
%Creating column vector using only colon operator
M32vec = M32sumAll(:)
allthesame = isequal(M32vec, M32vecP, M32vecR)

Output:

column format

3. Maintaining the shape of an array during assignment operation

The application of the assignment operator in the assignment operation on the input matrix, assures the shape of the array remains unchanged.

Code:

M32sumAll(:) = [1 2; 3 5]

Output:

assignment operation

Note: In order to maintain the shape of the input array, the number of elements being assigning to the input array should be the same as the number of elements in the existing array input. The values from the right-hand side get assigned to the input array in the left-hand side in the form of a column vector. A scalar value can also be used as the right-hand side operand in the assignment operation. In that case, MATLABapplies a scalar expansion for the left-hand side to be filled.

4. Working with all the entries in specified dimensions

The colon operator can also be used to manipulate specific dimensions of the input array.

Code:

M = zeros(3,3,3)M(:) = 1:numel(M)M1 = M(:,[2:size(M,2) 1],:)

Output:

entries in specified dimensions

Matlab colon11

Explanation: The indices of the elements in the input matrix ‘M’ is redefined and created new input matrix M1 with the application of the colon operator.

5. Create Unit-Spaced Vector

Using a colon operator, a vector having a list of consecutive numbers within a specified range, can be generated using the syntax li=j: k.

Code:

list_vector = 1:15

Output:

Unit-Spaced Vector

Explanation: A list of having a number from 1 to 15 is generated from the MATLAB command using a colon operator.

6. Create a Vector with Specified Increment

Using colon operator, a vector having a list of numbers having an equal difference between two consecutive numbers within a specified range can be generated using the syntax li=j:i: k.

Code:

list_vector = 1:3:15

Output:

Create a Vector

Explanation: A list of having numbers from 1 to 15 with a common difference of 3, are generated from the MATLAB command using a colon operator.

7. Specify for-loop Iterations

Colon operator is also used in designing the looping operation in MATLAB programming.

Code:

for m = 1:4
m^3+3
end

Output:

Specify for-loop Iterations

Additional point:

In case I being a non-integer and k is not equals to j+m*i, in the command form of li=j:i:k, floating-point arithmetic determines about colon including the endpoint k or not.

If no scalar array is specified, then MATLAB assumes i:k as j(1): i(1):k(1).

li = colon(j,k) and li = colon(j,i,k) are rarely used alternate ways for the commands li=j:k and li=j:i:k respectively.While implementing class, these syntaxes can be used to apply operator overloading.

Linspaceexhibit similar behavior to that of colon operator. The difference lies aslinespacegives direct control over the number of points and endpoints are always gets included.

Logspace, the sibling function is used to generate values being logarithmically spaced.

Recommended Articles

This is a guide to MATLAB Colon. Here we discuss an introduction to MATLAB Colon, syntax, examples and application. You can also go through our other related articles to learn more –

  1. MATLAB Indexing
  2. Matlab Struct
  3. MATLAB Plot Function
  4. Matrix in Matlab
Popular Course in this category
MATLAB Training (3 Courses, 1 Project)
  3 Online Courses |  1 Hands-on Project |  8+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

R Programming Training (13 Courses, 20+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more