Sliced Variables in "parfor"

1 次查看(过去 30 天)
Reza Teimoori
Reza Teimoori 2016-12-31
评论: Matt J 2016-12-31
I'd like to run a loop using parfor similar to this:
A = zeros(10,10,10,10);
parfor i = 1:100
ind = ceil(rand(1,4)*10);
A(ind) = A(ind) + 1;
end
This yields to an error due to the way indices of A is being defined in each iteration. I understand that one workaround can be to save the "ind" values in each iteration in a temporary variable and use them in a later regular loop to update the matrix A. But I was wondering if there is any way that lets me do the whole thing in a single parfor loop. In my case the matrices and indices that I'll be working with are so large that I can't save them for use in a later loop.
Thank you all!
  1 个评论
Matt J
Matt J 2016-12-31
Are you sure you didn't really mean as follows?
parfor i = 1:100
ind = ceil(rand(1,4)*10);
i=ind(1);
j=ind(2);
k=ind(3);
l=ind(4);
A(i,j,k,l) = A(i,j,k,l) + 1;
end

请先登录,再进行评论。

回答(1 个)

Matt J
Matt J 2016-12-31
Suppose you wanted to do this 5 million times:
A = zeros(10,10,10,10);
M=5;
N=1e6;
parfor i = 1:M
subs = ceil(rand(N,4)*10);
A=A+accumarray(subs,1,size(A));
end
Obviously there are different possible choices of partitioning parameters M,N. You want to make N as large as your RAM reasonably allows because that will use the most vectorization.

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by