Parfor error with bsxfun

1 次查看(过去 30 天)
Cem
Cem 2014-7-9
编辑: Matt J 2014-7-9
Dear all,
I'm having a weird issue with using parfor and bsxfun. I provide a minimal working example below (tried on both 2011b and 2013a). The version with for is working fine, whereas if I switch to parfor, I receive the 'non-singleton dimensions must match' error for bsxfun. I'm not sure if this was already answered although I looked for possible related questions&answers. Thanks in advance.
Cem
var1=repmat((1:1000)',2,3);
var2=ones(1000,1000);
var3=ones(6000,1);
temporary_var=bsxfun(@times,var2(var1,:),var3);
result=zeros(1000,1000);
for i=1:1000
result(i,:)=sum(bsxfun(@times,var2(var1,i),temporary_var));
end
Note: the issue persists both matlabpools open and closed.

采纳的回答

Jill Reese
Jill Reese 2014-7-9
I think that the parfor analysis is getting a bit confused by the multidimensional indexing into var2. If you re-write the code as follows it should work:
var1=repmat((1:1000)',2,3);
var2=ones(1000,1000);
var3=ones(6000,1);
temporary_var=bsxfun(@times,var2(var1,:),var3);
result=zeros(1000,1000);
tmp = var2(var1,:);
parfor i=1:1000
result(i,:)=sum(bsxfun(@times,tmp(:,i),temporary_var));
end
  1 个评论
Matt J
Matt J 2014-7-9
编辑:Matt J 2014-7-9
@Cem,
If this is what you want, the parfor loop looks like a doubtful way to go about it. A loop-free computation of the same result is
result=tmp.'*temporary_var;
which would likely have better internal optimization/parallelization than parfor.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by