title: Triple weighted sum
body: <p>I was trying to vectorize a certain weighted sum but couldn't figure out how to do it. I have created a simple minimal working example below. I guess the solution involves either bsxfun or reshape and kronecker products but I still have not managed to get it working.</p>
  
  <pre><code>rng(1);
  N = 200;
  T1 = 5;
  T2 = 7;
  T3 = 10;
  
  
  A = rand(N,T1,T2,T3);
  w1 = rand(T1,1);
  w2 = rand(T2,1);
  w3 = rand(T3,1);
  
  B = zeros(N,1);
  
  for i = 1:N
   for j1=1:T1
    for j2=1:T2
     for j3=1:T3
      B(i) = B(i) + w1(j1) * w2(j2) * w3(j3) * A(i,j1,j2,j3);
     end
    end
   end
  end
  
  A = B;
  </code></pre>
  
  <p>For the two dimensional case there is a smart answer <a href="https://stackoverflow.com/questions/39108702/vectorize-weighted-sum-matlab/39108811#39108811">here</a>.</p>