Cell array use, indexing and slicing in parallelization
Mostrar comentarios más antiguos
I have a problem with the parfor loop below. Essentially I get the following error: "Error: Unable to classify the variable 'inter_cnt' in the body of the parfor-loop."
I have tried to include enough information so that other variables make sense, while not distracting from the point.
I think it might be a problem with "slicing", because I know that you need to slice your variables properly when using a parfor.
If I comment out the "inter_cnt(...)" line (see code for relevant comment at the end of the while loop), then the code runs.
Please let me know if anymore information is necessary
% initializing first page of cell array
inter_cnt={'energy',enr(1);'coherent', 0;'compton', 0;'photoelectric', 0;'pair-triplet', 0;};
% re setting interaction counter for all other energies (pages)
for i= 2:length(enr)
inter_cnt_intermediate = {'energy',enr(i);'coherent', 0;'compton', 0;'photoelectric', 0;'pair-triplet', 0;};
inter_cnt=cat(3,inter_cnt,inter_cnt_intermediate);
end
N_particles = 0;
parfor j = 1:length(enr)
particle_num = 0;
while particle_num <= N_particles % a while loop that loops for each step the particle takes
% select the interaction
intr_gen = rand; % generate a random number
inter_type = interaction_selector(enr(j),intr_gen,M,mu_t); % call the interaction selector function
index_for_cell_interaction = find(strcmp([inter_cnt(:,1,1)], inter_type)); % finds the right index in the cell array containing the counts of each interaction
index_for_cell_energy = find([inter_cnt{1,2,:}] == enr(j)); % finds the right index in the cell array containing the counts of each interaction
inter_cnt{index_for_cell_interaction,2,index_for_cell_energy} = inter_cnt{index_for_cell_interaction,2,index_for_cell_energy} + 1; % increases the count for the interaction type that was chosen by interaction selector
% commenting out the above ^ line, the code runs without error
particle_num = particle_num+1;
end
end
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!