where are colon operator used in Matlab? | applied electronics engineering

Latest

where are colon operator used in Matlab?

By Applied Electronics - Wednesday, March 15, 2017 No Comments
Colon operator is one of the important operator in Matlab. It is used in variety of program statements. People can get confused and unable to distinguish clearly where exactly should colon operator be used.

Colon operator is used in matlab for (1) creating vectors and matrices, (2)to specify sub-matrices and to (3) perform iteration.

Colon operator can be used make a vector with equal spacing of 1 unit as follows.

v = 1:6
means a vector
v =
1 2 3 4 5 6

In the above example the default 1 unit increment is used. But we can also have negative, positive and non-unity increment. For example we can use negative decrement as follows,

v = 3:-0.5:1
v=
3.0000 2.5000 2.0000 1.5000 1.0000

The following statement is used to create matrix using colon,
t = [(0:2:10):(5:-0.2:4)]

t =
0 2.0000 4.0000 6.0000 8.0000 10.0000
5.0000 4.8000 4.6000 4.4000 4.2000 4.0000

Finally, the colon operator is also used for iteration. An example is its use in For loop. For example, suppose we would like to find the squares of all the integers starting from 1 to 100. We could use the following statements to solve the problem:

sum = 0;
for i = 1:100
sum = sum + i^2;
end
sum

Further example is given in Example of using colon operator in Matlab.

No Comment to " where are colon operator used in Matlab? "