Sliced Variables in "parfor"
1 view (last 30 days)
Show older comments
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 Comment
Matt J
on 31 Dec 2016
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
Answers (1)
Matt J
on 31 Dec 2016
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.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!