9/15: MATLAB Basics - Vectors, Colon Operator, Indexing, and Dot Product

Data series and length

  • A sequence is a data series; understanding its length is essential for operations on it.

Vector creation and sequences

  • Colon operator: t=start:step:endt = start:step:end creates a vector from start to end with uniform increments.
    • Example: t=0:0.5:2t = 0:0.5:2[0,0.5,1,1.5,2][0,\, 0.5,\, 1,\, 1.5,\, 2] (five points)
  • Alternative: extlinspace(a,b,N)ext{linspace}(a,b,N) creates N evenly spaced points from aa to bb (inclusive).
    • Example: extlinspace(2,4,5)=[2,2.5,3,3.5,4]ext{linspace}(2,4,5) = [2,\, 2.5,\, 3,\, 3.5,\, 4]
  • When using colon operator, the meaning of the last value and increment matters:
    • If you specify a fixed end (e.g., 2 or 4) with a given step, MATLAB stops at or before the end value.
    • If you change the end value, the number of elements may change accordingly.

End-point behavior and counting

  • Using a start:step:end with odd/even bounds changes how many elements you get:
    • Example: odd numbers 1:2:20 yields [1,3,5,7,9,11,13,15,17,19][1,3,5,7,9,11,13,15,17,19] (last is 19, since 20 would exceed the step from 1).
    • If you use 1:2:21, you get one more element (includes 21).
  • This is why the last number can affect the count but not the pattern (odd numbers in this case).

Indexing: extracting columns and rows

  • For a matrix AA, you can extract:
    • Entire column j: A(:,j)A(:,j)
    • Entire row i: A(i,:)A(i,:)
  • Example usage patterns:
    • To get all elements of column 3: A(:,3)A(:,3)
    • To get all elements of row 1: A(1,:)A(1,:)

Basic matrix operations and dot product

  • Multiplying a row by a column gives a scalar (dot product):
    • If AA is 1imesN1 imes N and BB is Nimes1N imes 1, then AimesBA imes B' is a scalar.
  • Example (dot product):
    • Let A=[120]A = [1 \, 2 \, 0] and B=[021]opB = [0 \, 2 \, 1]^ op,
      then AimesB=1<em>0+2</em>2+01=4.A imes B' = 1<em>0 + 2</em>2 + 0*1 = 4.

Zeros and ones (common utilities)

  • Create matrices of zeros or ones for placeholders or masking:
    • Z=extzeros(m,n)Z = ext{zeros}(m,n)
    • O=extones(m,n)O = ext{ones}(m,n)

Quick takeaways

  • Use the colon operator for simple sequences and linspace for exact count control.
  • Indexing with (:, j) and (i, :) is the core pattern for column/row extraction.
  • Dot products are achieved via multiplying a row vector by a column vector (or using the transpose).