Friday 22 August 2014

New uses for Matrix Multiplication


I wish I'd known MATLAB had...     matrix multiplication
  • Often overlooked, matrix multiplication can do many useful things!
  • Using multiplication instead of repmat. If you have a column vector V, and you want to repeat it horizontally 5 times,
    • REPMAT(V, 1, 5)  is the same as
              V*[1 1 1 1 1].
    • REPMAT(V,1,10) is the same as
             V*ones(1,10)
    • REPMAT(V, 1, length(V)) will make a square, and so will V*(1+0*V)
    • You can create the times table for 1 to 12 with [1:12]'*[1:12]
  • Using multiplication instead of SUM. If you have a column vector V, and you want to sum it,
    • SUM(V)  is the same as (1+0*V)'*V     note that 0*V is a handy way of creating a zero matrix of the same size as V.
    • if you want a weighted sum, where W is the weights, 
      SUM(V.*W) is the same as V'*W   or W'*V.

No comments:

Post a Comment