how to slice variables to use parfor
조회 수: 5 (최근 30일)
이전 댓글 표시
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! :)
댓글 수: 0
채택된 답변
Matt J
2021년 3월 20일
편집: Matt J
2021년 3월 20일
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]);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!