Wednesday 2 October 2013

Basic Matrix Operations

  • Mathematical Operators
    • May be different for scalars versus arrays versus combinations of both
    • +, -, *, /, ^, \
      • Left division is only relevant to matrix operations:
        • A x = b
        • x = A \ b
    • Elementwise - .*, ./, .^ - Acts on matrices element by element, as opposed to normal matrix operations.
  • 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
  • A matrix referenced with a single subscript is treated as if it were "unwound" column by column into a vector form. E.g. "data( 4 ) = 7" continuing from the above example.
  • Rows or columns of a matrix can be deleted by replacing them with the null vector:
    • data( 2, : ) = [ ];
    • data( :, 3 ) = [ ];
    • Individual elements can only be deleted if the matrix is unwound first: data( 5 ) = [ ];
  • Elementary Matrix operations:
    • See "help elmat" for a full list
    • repmat
  • Basic array information
    • size - rows & columns for a matrix
    • length - columns only for a matrix
    • ndims - numberof dimensions.
    • numel - number of elements
    • all - generates 1 if all elements are non-zero, 0 otherwise.
    • any - genrates 1 if any element is non-zero, 0 otherwise.
  • Matrix Manipulation
    • cat - Concatenate
    • reshape
    • diag - Extracts the diagonal of a matrix into a vector, or creates a matrix having the vector as its diagonal.
    • fliplr - Flips a matrix left to right.
    • flipud - Flips a matrix up and down.
    • rot90 - counterclockwise
    • tril - Extracts lower triangular part
    • triu - Extracts upper triangular part
      • ( NOT the same as LU factorization - See "lu" if you are interested in that. )

No comments:

Post a Comment