How would you vectorize this nested loop in matlab/octave? -


i stuck @ vectorizing tricky loop in matlab/octave:

[nr, nc] = size(r); p = rand(nr, k); q = rand(k, nc);  = 1:nr    j = 1:nc       if r(i,j) > 0           eij = r(i,j) - p(i,:)*q(:,j);           k = 1:k           p(i,k) = p(i,k) + alpha * (2 * eij * q(k,j) - beta * p(i,k));           q(k,j) = q(k,j) + alpha * (2 * eij * p(i,k) - beta * q(k,j));           end       end    end end 

the code tries factorize r p , q, , approaching nearest p , q update rule. example, let r = [3 4 0 1 1; 0 1 0 4 4; 5 4 3 1 0; 0 0 5 4 3; 5 3 0 2 1], k=2, alpha=0.01 , beta=0.015. in real case, use huge sparse matrix r (that's why need vectorization), , k remain small (less 10). goal of whole script producing prediction value every 0 elements in r, based on non 0 elements. got code here, written in python.

this looks 1 of cases not code can vectorized. still, can make bit better now.

[nr, nc] = size(r); p = rand(nr, k); q = rand(k, nc);  = 1:nr    j = 1:nc       if r(i,j) > 0           eij = r(i,j) - p(i,:)*q(:,j);           p(i,:) = p(i,:) + alpha * (2 * eij * q(:,j)' - beta * p(i,:));           q(:,j) = q(:,j) + alpha * (2 * eij * p(i,:)' - beta * q(:,j));       end    end end 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -