Skip to main content

4.8) Accessing Data


  • Arrays contain data. We can access parts of an array using the index numbers (or indices)
  • Index numbers represent specific positions within an array. For example, let's first create a column vector with 40 elements
  • [matlab]
    >> a=[0.25:0.25:10]
    [/matlab]

  • Now let's access elements of the vector
  • [matlab]
    >> a(5)
    ans=1.25
    [/matlab]


    [matlab]
    >> a(10:12)
    ans= 2.50 2.75 3.00
    [/matlab]

    Let's access the 1st, 5th and 25th elements of the vector.

    [matlab]
    >> a([1,5,25])
    [/matlab]

    Let's access the 1st, 3rd, 5th and 7th elements of the vector.

    [matlab]
    >> a([1:2:7])
    ans= 0.25 0.75 1.25 1.75
    [/matlab]


    [matlab]
    >> a(41)
    Index exceeds matrix dimensions
    [/matlab]

    We cannot access the 41st element of the vector because the vector has only 40 elements.