Wednesday 2 October 2013

Creating Matrices in MATLAB

Direct Entry

  • [ list ] - specify values. Comma to separate numbers, semicolon to separate rows.
  • Trying to READ an element of an array that is out of bounds will generate an error, but WRITING to one will simply expand the array to accommodate the new data ( so long as the indices are positive. ) For example:
            data = ones( 2, 2 ); % Creates a 2 x 2 array of value 1
            whos
            x = data( 3, 3 ); % Error - element doesn't exist.
            whos
            data( 3, 3 ) = 42 % Sets the value and expands the array, filling with zeros.
            whos

Colon Operator

  • Colon operator - Fill in using a given increment ( 1 by default)

Subset or Combination of other arrays

  • Can also concatenate matrices.

Using Matlab Functions

  • linspace, logspace - Fill in linearly or logarithmically using endpoints and number of points
  • Functions ( see below ) - eye, magic, peaks, rand, diag, etc.
  • Matrices may be "tiled" using "repmat( array, nRows, nCols )"
  • zeros
  • ones
  • eye
  • linspace
  • logspace
  • repmat
  • See "help elmat" for a full list

Load from a file

No comments:

Post a Comment