1
我有以下的短Matlab代碼:PARFOR在for循環
res = cell(10*100,1);
for i = 1:10
parfor j = 1:100
idx = ((i-1) * 100) + j;
res(idx) = 5;
end
end
我得到一個錯誤的res(idx) = 5;
。如果我不在parfor循環中使用變量i,它可以工作,但我必須跟蹤i。
我該怎麼辦?
編輯:我已經解決了。
res = zeros(10*100,1);
for i = 1:10
temp = zeros(100,1);
parfor j = 1:100
a = i;
temp(j) = data((i-1) * 100) + j);
end
res((i-1)*100+1:i*100) = temp;
end
爲什麼不從1:(10 * 100)''單個'parfor'循環做? – Raab70
這將是一種可能性,但我會在集羣上運行它,而且我很可能不會分配這麼多的並行循環。是否沒有可能在parfor循環中創建變量i的值? – machinery
你有沒有讀過[this](https://www.mathworks.com/help/distcomp/nesting-and-flow-in-parfor-loops.html)? – Raab70