如何在 matlab 中向量化异或运算
我已经在 Matlab 的 Profiler 中运行了以下代码,对我来说向量化这段代码非常重要,因为我觉得这是一个不必要的 for 循环。
我有 2 个矩阵 G 和 source_data。 G 中的每一列将确定我需要从 source_data 中选取的行并将它们异或在一起。
我正在使用以下代码创建 G 和 source_data
for i=1:10
source_data(i,:)=rand(1,20)<.8;
end
for i=1:15
G(:,i)=rand(10,1)<.9;
end
我正在使用下面的 for 循环执行异或运算:
z=1;
while(i<=15)
for j=1:10
if(G(j,i)==1)
intersum(z+1,:)=xor(intersum(z,:), source_data(j,:));
z=z+1;
end
end
C(i,:)=intersum(z,:);
i=i+1;
end
有没有办法向量化此代码?对于小矩阵来说,时间延迟是可以接受的,但对于大矩阵来说,这个代码效率很低。
谢谢,
巴维亚
I have run the following code in Profiler in Matlab and it is quite essential for me to vectorise this code as I feel that this is an unnecessary for loop.
I have 2 matrices G and source_data.
every column in G will determine the rows which I need to pick up from source_data and xor them together.
I am creating G and source_data using the following piece of code
for i=1:10
source_data(i,:)=rand(1,20)<.8;
end
for i=1:15
G(:,i)=rand(10,1)<.9;
end
I am performing an xor operation using the for loop below:
z=1;
while(i<=15)
for j=1:10
if(G(j,i)==1)
intersum(z+1,:)=xor(intersum(z,:), source_data(j,:));
z=z+1;
end
end
C(i,:)=intersum(z,:);
i=i+1;
end
Is there a way to vectorise this code ? The time lag is acceptable for a small matrix but for large matrices this code is quite in efficient.
Thanks,
Bhavya
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设:
这是代码的矢量化形式,它产生与原始结果完全相同的结果:
为了检查两个版本的时间,我使用了这个测试函数:
我在我的机器上得到了以下时间:
现在我为什么要这样改变它......
对
逻辑
数组的xor
操作几乎就是检查总和的奇偶校验(处理true
值为 1)。此外,intersum
被用作累加器,因此 who 的值最终会出现在C
中,因此我们完全跳过它。获取 G(j,i) 为 1 的行可以通过 逻辑索引。最后,即使您不喜欢这个提议的版本,我也建议您预先分配您的
C
和intersum
向量(如果您还没有这样做) 。这对我过去来说有很大的影响。Assuming that:
Here's a vectorized form of you code that produces the exact same result as your original:
To check the timing of both versions I used this test function:
And I got the following timings on my machine:
Now to why I changed it that way...
A
xor
operation over an array oflogical
s is pretty much checking the parity of the sum (treatingtrue
values as 1). Furhtermore,intersum
is being used as an accumulator, so there's who's values eventually ends up inC
so we skip it altogether. Taking the rows for whichG(j,i)
is 1 can be done by logical indexing.And finally, even if you don't like this proposed version, I'd recommend preallocating your your
C
andintersum
vectors (in case you're not doing so already). That has made a lot of difference for me in the past.