matlab循环矢量化 嵌套,在matlab/octave中,如何對這個嵌套循環進行矢量化?

该博客讨论了在MATLAB/Octave中优化复杂循环的问题,特别是涉及矩阵R的分解和逼近过程。作者提供了一个用于分解R为P和Q的代码示例,并尝试通过更新规则寻找最接近的P和Q。针对大型稀疏矩阵R的处理,作者提出了两种可能的优化方案,包括减少内层循环和利用非零元素索引。整个脚本的目标是根据非零元素预测R中的零元素值。博客内容源于Python代码并探讨了如何在MATLAB/Octave环境中实现。
摘要由CSDN通过智能技术生成

I am stuck at vectorizing this tricky loop in MATLAB/Octave:

在MATLAB/Octave中,我被困在矢量化這個復雜的循環中:

[nr, nc] = size(R);

P = rand(nr, K);

Q = rand(K, nc);

for i = 1:nr

for j = 1:nc

if R(i,j) > 0

eij = R(i,j) - P(i,:)*Q(:,j);

for 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 to factorize R into P and Q, and approaching the nearest P and Q with an update rule. For 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 and beta=0.015. In my real case, I will use a huge sparse matrix R (that's why I need vectorization), and K remain small (less than 10). The goal of the whole script is producing a prediction value for every 0 elements in R, based on the non zero elements. I got this code from here, originally written in Python.

代碼試圖將R分解為P和Q,並通過更新規則接近最近的P和Q。例如,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, =0。01,=0。015。在我的實際情況中,我將使用一個巨大的稀疏矩陣R(這就是為什么我需要矢量化),而K仍然很小(小於10)。整個腳本的目標是根據非零元素,為R中的每一個0元素生成一個預測值。我從這里得到了這個代碼,最初是用Python編寫的。

2 个解决方案

#1

1

This looks like one of those cases that not all code can be vectorized. Still, you can make it a bit better than it is now.

這看起來像那些不是所有代碼都可以量化的例子。不過,你可以讓它比現在好一點。

[nr, nc] = size(R);

P = rand(nr, K);

Q = rand(K, nc);

for i = 1:nr

for 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

#2

1

Since the operations on P and Q are serial in nature (iterative updates) I do not think you can do much better. You can save the if in the loop:

由於P和Q的操作在本質上是串行的(迭代更新),所以我認為您不能做得更好。您可以在循環中保存if語句:

[nr, nc] - size(R);

P = rand(nr, K);

Q = rand(K, nc);

[nzi nzj] = find( R > 0 );

for ii=1:numel(nzi)

i = nzi(ii);

j = nzj(ii);

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值