Array indexing in parfor loops, why does this simple code not work?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Right Grievous
el 19 de Jul. de 2014
Comentada: Right Grievous
el 19 de Jul. de 2014
Hi everybody,
I have recently been looking into parfor loops, mostly out of curiosity.
I have lots of loops which follow this syntax:
A = {};
B = [1 3 6];
parfor i = 1:3
A{B(i)} = i;
end
A
I don't understand why this code produces an error, I know that the order in which the calculations may be carried out are random but both A and B are defined outside the loop, the index into both is given by the loop number 'i' and the results can be entered into A randomly as it's an array. So why does this code fail?
Any help would be greatly appreciated,
Rod.
EDIT
My problem is basically that I have input matrices or vectors and for loops which I want to swap to parfor, but often I only want to access certain parts of each input matrix or vector. parfor won't accept a non continuous index like parfor i = [1 3 6], but you also can't use [1 3 6] as an index inside the loop because values might be duplicates.
The solution I've found is the inclusion of an if statement:
A = {};
parfor i = 1:6
if i == 1||i == 3||i == 6
A{i} = i;
end
end
A
Serious coders here will probably laugh at me, but it does what I want.
0 comentarios
Respuesta aceptada
Jos
el 19 de Jul. de 2014
You're getting the cell index for A out of vector B, however B could be defined with multiple elements having the same value (e.g. B=[1 1 6];), if this would occur multiple parallel instances would be attempting to access the same cell of A at the same time
5 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!