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:end creates a vector from start to end with uniform increments.
    • Example: t = 0:0.5:2 → [0,\, 0.5,\, 1,\, 1.5,\, 2] (five points)
  • Alternative: ext{linspace}(a,b,N) creates N evenly spaced points from a to b (inclusive).
    • Example: 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] (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 A, you can extract:
    • Entire column j: A(:,j)
    • Entire row i: A(i,:)
  • Example usage patterns:
    • To get all elements of column 3: A(:,3)
    • To get all elements of row 1: A(1,:)

Basic matrix operations and dot product

  • Multiplying a row by a column gives a scalar (dot product):
    • If A is 1 imes N and B is N imes 1, then A imes B' is a scalar.
  • Example (dot product):
    • Let A = [1 \, 2 \, 0] and B = [0 \, 2 \, 1]^ op,
      then A imes B' = 10 + 22 + 0*1 = 4.

Zeros and ones (common utilities)

  • Create matrices of zeros or ones for placeholders or masking:
    • Z = ext{zeros}(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).