title: Vectorizing a weighted sum of matrices in MATLAB
body: <p>I'm trying to vectorize the following operation in MATLAB, but it's got me stumped. I've learned from experience that there usually <em>is</em> a way, so I'm not giving up just yet. Any help would be appreciated.</p>
  
  <p>I have a collection of <code>m</code> row-vectors each of size <code>n</code>, arranged in an <em>m</em> x <em>n</em> matrix; call it <code>X</code>.</p>
  
  <p>I also have an m-sized vector of weights, <em>w</em>.</p>
  
  <p>I want to compute a weighted sum of the matrices formed by the self outer products of the vectors in <code>X</code>.</p>
  
  <p>Here is a MWE using a <code>for</code> loop:</p>
  
  <pre><code>m = 100;
  n = 5;
  
  X = rand(m, n);
  w = rand(1, m);
  
  S = zeros(n, n);
  for i = 1 : m
      S = S + (w(i) * X(i, :)' * X(i, :));
  end
  
  S
  </code></pre>