Azzera filtri
Azzera filtri

Sliced Variables in "parfor"

1 visualizzazione (ultimi 30 giorni)
Reza Teimoori
Reza Teimoori il 31 Dic 2016
Commentato: Matt J il 31 Dic 2016
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 Commento
Matt J
Matt J il 31 Dic 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

Accedi per commentare.

Risposte (1)

Matt J
Matt J il 31 Dic 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.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by