Filter löschen
Filter löschen

Sliced Variables in "parfor"

1 Ansicht (letzte 30 Tage)
Reza Teimoori
Reza Teimoori am 31 Dez. 2016
Kommentiert: Matt J am 31 Dez. 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 Kommentar
Matt J
Matt J am 31 Dez. 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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt J
Matt J am 31 Dez. 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.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by