Skip to main content

4.5) Creating Vectors And Matrices: Colon Operator


  • An alternate method of creating vectors is to use the colon operator (:)
  • As we will see, the colon operator is one of the most useful Matlab commands you will come across
  • The colon operator can be used to create a list of evenly spaced numbers. The default separation (or stride) is 1. So for example:
  • [matlab]
    >> b=1:10;
    [/matlab]

    will create a vector named "b" which is the same as vector "a". You could also specify an alternate stride. So for example:

    [matlab]
    >> c=1:2:10
    [/matlab]

    will generate a vector "c", whose elements begin at 1 and increase by 2 until it gets to 10. So now "c" is equals to:

    [matlab]
    c= [1 3 5 7 9]
    [/matlab]

  • To recap, the colon operator (:) is used to specify contiguous indices, the two forms of the operator are:
  • start : end
    start : stride : end