how to slice variables to use parfor

5 visualizaciones (últimos 30 días)
endystrike
endystrike el 20 de Mzo. de 2021
Comentada: endystrike el 20 de Mzo. de 2021
Hi,
I cannot convert this for loop into a parfor loop: I tried to have a look to the help but I cannot understand how to slice this variable to use the parfor istead of the for loop.
tot_rows = randi([10000,20000],1);
tot_cols = randi([10,500],1);
data = zeros(tot_rows, tot_cols);
for k=1:tot_cols
idx = randi([1 tot_rows], randi([1 tot_rows]), 1); %now is generated randomly, then in real version idx is found with a criteria...
tmp = rand(length(idx),1);%now is generated randomly, then in real version tmp is taken from txt file...
data(idx,k) = tmp;
end
If I try to use the parfor loop I get the following error, I suppose because "data" array cannot be recalled in this way but I cannot understand how to slice it:
tot_rows = randi([10000,20000],1);
tot_cols = randi([10,500],1);
data = zeros(tot_rows, tot_cols);
parfor k=1:tot_cols
idx = randi([1 tot_rows], randi([1 tot_rows]), 1); %now is generated randomly, then in real version idx is found with a criteria...
tmp = rand(length(idx),1);%now is generated randomly, then in real version tmp is taken from txt file...
data(idx,k) = tmp;
end
Error using test_for (line 6)
Error: Unable to classify the variable 'data' in the body of the parfor-loop. For more information, see Parallel for Loops in MATLAB, "Solve Variable Classification Issues in parfor-Loops".
Thanks everyone for the help! :)

Respuesta aceptada

Matt J
Matt J el 20 de Mzo. de 2021
Editada: Matt J el 20 de Mzo. de 2021
One way,
[I,J,S]=deal(cell(tot_cols,1));
parfor k=1:tot_cols
idx = randi([1 tot_rows], randi([1 tot_rows]), 1); %now is generated randomly, then in real version idx is found with a criteria...
tmp = rand(length(idx),1);%now is generated randomly, then in real version tmp is taken from txt file...
[I{k} ,J{k},S{k}]=deal(idx,idx,tmp);
J{k}(:)=k;
end
data = accumarray(cell2mat([I,J]), cell2mat(S),[tot_rows, tot_cols]);

Más respuestas (0)

Categorías

Más información sobre Parallel for-Loops (parfor) en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by