MATLAB - Colon Notation (2024)

MATLAB - Colon Notation (1)

'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });

The colon(:) is one of the most useful operator in MATLAB. It is used to create vectors, subscript arrays, and specify for iterations.

If you want to create a row vector, containing integers from 1 to 10, you write −

1:10

MATLAB executes the statement and returns a row vector containing the integers from 1 to 10 −

ans = 1 2 3 4 5 6 7 8 9 10 

If you want to specify an increment value other than one, for example −

100: -5: 50

MATLAB executes the statement and returns the following result −

ans = 100 95 90 85 80 75 70 65 60 55 50

Let us take another example −

0:pi/8:pi

MATLAB executes the statement and returns the following result −

ans = Columns 1 through 7 0 0.3927 0.7854 1.1781 1.5708 1.9635 2.3562 Columns 8 through 9 2.7489 3.1416

You can use the colon operator to create a vector of indices to select rows, columns or elements of arrays.

The following table describes its use for this purpose (let us have a matrix A) −

FormatPurpose
A(:,j)is the jth column of A.
A(i,:)is the ith row of A.
A(:,:)is the equivalent two-dimensional array. For matrices this is the same as A.
A(j:k)is A(j), A(j+1),...,A(k).
A(:,j:k)is A(:,j), A(:,j+1),...,A(:,k).
A(:,:,k)is the kth page of three-dimensional array A.
A(i,j,k,:)is a vector in four-dimensional array A. The vector includes A(i,j,k,1), A(i,j,k,2), A(i,j,k,3), and so on.
A(:)is all the elements of A, regarded as a single column. On the left side of an assignment statement, A(:) fills A, preserving its shape from before. In this case, the right side must contain the same number of elements as A.

Example

Create a script file and type the following code in it −

A = [1 2 3 4; 4 5 6 7; 7 8 9 10]A(:,2) % second column of AA(:,2:3) % second and third column of AA(2:3,2:3) % second and third rows and second and third columns

When you run the file, it displays the following result −

A = 1 2 3 4 4 5 6 7 7 8 9 10ans = 2 5 8ans = 2 3 5 6 8 9ans = 5 6 8 9

Advertisem*nts

';adpushup.triggerAd(ad_id); });

MATLAB - Colon Notation (2024)

FAQs

What is colon notation in MATLAB? ›

Description. The colon is one of the most useful operators in MATLAB®. It can create vectors, subscript arrays, and specify for iterations. example. x = j : k creates a unit-spaced vector x with elements [j,j+1,j+2,...,j+m] where m = fix(k-j) .

What is the colon operator in a matrix? ›

Colon operators perform element-by-element operations. The matrices above are said to be c-conformable; the c stands for colon. The matrices have the same number of rows and columns, or one or the other is a vector with the same number of rows or columns as the matrix, or one or the other is a scalar.

What is the Linspace colon in MATLAB? ›

linspace is similar to the colon operator, “ : ”, but gives direct control over the number of points and always includes the endpoints. “ lin ” in the name “ linspace ” refers to generating linearly spaced values as opposed to the sibling function logspace , which generates logarithmically spaced values.

What is the range notation in MATLAB? ›

Description. y = range( X ) returns the difference between the maximum and minimum values of sample data in X . If X is a vector, then range(X) is the range of the values in X . If X is a matrix, then range(X) is a row vector containing the range of each column in X .

What does a colon (:) mean in math? ›

A ratio is two numbers separated by a colon (:). A colon indicates the need to divide. A proportion is two ratios of equal value. A proportion can be written as two fractions or ratios on either side of a double colon (::).

What is the notation of colon? ›

The colon, :, is a punctuation mark consisting of two equally sized dots aligned vertically. A colon often precedes an explanation, a list, or a quoted sentence.

What does a colon (:) do? ›

A colon is used to give emphasis, present dialogue, introduce lists or text, and clarify composition titles. Emphasis—Capitalize the first word after the colon only if it is a proper noun or the start of a complete sentence. (She had one love: Western Michigan University.)

What does colon (:) do in a list? ›

The colon is used chiefly to introduce a list, quotation, or explanation following an independent clause (complete sentence). Use a colon at the end of a complete statement to introduce a list. Example: I have a few favorite classes at LLCC: literature, psychology, and art.

What does colon do in array? ›

The best way to think about array indices is that they are the number of offsets from the first position — the first element does not require an offset to get to. Note that we can index a range using the colon ( : ) operator. A colon by itself means fetch everything.

How to write a MATLAB function? ›

Syntax for Function Definition
  1. function myOutput = myFunction(x) If your function returns more than one output, enclose the output names in square brackets.
  2. function [one,two,three] = myFunction(x) If there is no output, you can omit it.
  3. function myFunction(x) Or you can use empty square brackets.

How to create a matrix in MATLAB? ›

To create a matrix that has multiple rows, separate the rows with semicolons. Another way to create a matrix is to use a function, such as ones , zeros , or rand . For example, create a 5-by-1 column vector of zeros.

What is the alternative to Linspace in MATLAB? ›

The logspace function is especially useful for creating frequency vectors. The function is the logarithmic equivalent of linspace and the ' : ' operator.

What is the dot notation in MATLAB? ›

Dot notation is a way to identify data at a specific level of the Stateflow® chart hierarchy. The first part of a qualified data name identifies the parent object. Subsequent parts identify the children along a hierarchical path. For example, in this chart, the symbol data resides in the substate aa of the state a .

What is the precision of MATLAB? ›

Increase Precision of Results

By default, MATLAB® uses 16 digits of precision. For higher precision, use vpa . The default precision for vpa is 32 digits. Increase precision beyond 32 digits by using digits .

How do you write range notation? ›

Use brackets, [], when the endpoints are included and parentheses, (), when the endpoints are excluded. Using the graph above, the range would be: R:(−1,2] Because −1 is not included we use a parentheses. We start from the bottom and write the intervals that y is defined on.

What does (:) do in MATLAB? ›

x(:) reshapes your matrix. The same is what is obtained when x is a row vector. so in general, x(:) lists the elements of x as column vector. This operator tells matlab to try to convert the data type also.

What is colon in function notation? ›

It's just a separator between the name of the function and its domain/range. In other contexts, like ∃x:P(x) it can be read exactly that way (there exists an x such that P(x)). A function definition like f:N→N can be read aloud as f is a function mapping N to itself.

What is the colon in set notation? ›

denotes the set of all elements in which have the property . This is called set builder notation. The colon is read as "such that".

What does a colon do in code? ›

In programming languages such as JavaScript, PHP and Python, colons are often used as indicators of control structures such as conditionals or loops; they define where a particular block starts and ends so that code within it can be executed only when its conditions have been met.

Top Articles
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 6187

Rating: 4.4 / 5 (65 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.