Converting nested FOR loops to PARFOR loop matlab

扶醉桌前 提交于 2019-12-30 11:15:48

问题


I have these nested for-loops that I would like to convert to parfor:

row = 1;
for i = 5 : 0.2 : 5.4
    col = 1;
    for j = 2 : 0.5 : 2.5
        matrx(row, col) = i * j;
        col = col + 1;
    end
    row = row + 1;
end

Does anyone any way in which this would be possible?


回答1:


I hope you're only displaying an extremely simplified version of your code, but anyways, the secret to parfor can be found by listening to Matlab numerous messages and reading the documentation. Start by learning good Matlab coding practices, and streamlining your code in such a way to fit your data into what Matlab wants in a parfor loop.

Things to note:

  1. Parfor loops should be integers.
  2. All matrices must be classified (read the documentation).
  3. Container matrices should be used in nested for loops

This is one way I would do it, although it depends on your final application

iVal = 5 : 0.2 : 5.4;
jVal = 2 : 0.5 : 2.5;

iLen = length(iVal);
jLen = length(jVal);

matrx = zeros(iLen, jLen);

parfor i = 1:iLen
    dummy = zeros(1, jLen);
    for j = 1:jLen
        dummy(j) = iVal(i) * jVal(j);
    end
    matrx(i,:) = dummy;
end


来源:https://stackoverflow.com/questions/13444745/converting-nested-for-loops-to-parfor-loop-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!