Borrar filtros
Borrar filtros

Sliced Variables in "parfor"

1 visualización (últimos 30 días)
Reza Teimoori
Reza Teimoori el 31 de Dic. de 2016
Comentada: Matt J el 31 de Dic. de 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 comentario
Matt J
Matt J el 31 de Dic. de 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

Iniciar sesión para comentar.

Respuestas (1)

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

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by