Vectorizing equation in MATLAB I am working on collaborative filtering using matrix factorization in MATLAB. I am using Gradient Descent for parameter learning. The cost function to optimize is :

$ J = {\left\| I \odot (R - U V') \right\|}_{F}^{2} + \lambda_1 {\left\| U \right\|}_{F}^{2} + \lambda_2 {\left\| V \right\|}_{F}^{2} $

I have the following equations for updates :

$ \nabla J_{u(i)} = -\sum_j I_{ij} (r_{ij} - u_i v_j') v_j $ 

$ \nabla J_{v(i)} = -\sum_j I_{ij} (r_{ij} - u_i v_j') u_i $

The dimensions of the matrices are :

$I  -&gt; n * n$

$R -&gt; n*n$

$U -&gt; n*k$

$V -&gt; n*k$

I am not able to come up with vectorized form of the gradient, so I have to loop over i's and j's explicitly, which is slowing down the code a lot.
Is your question how to vectorize the equations for updating?  You have a vector of i and j, and for each update you are summing over j.  Calculate the vectors of each of the components and then sum the vector over j.

Providing actual code might help your question.  As is, it seems vague.