将嵌套的FOR循环转换为PARFOR循环matlab [英] Converting nested FOR loops to PARFOR loop matlab
问题描述
row = 1; 我有这些嵌套的for循环,我想转换为parfor:
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
有没有人可以这样做?
$我希望你只是展示了一个极其简化的代码版本,但无论如何,通过聆听Matlab可以找到parfor的秘密消息和阅读文档。从学习好的Matlab编程实践开始,以这种方式精简你的代码,使你的数据符合Matlab所要求的 parfor
循环。 需要注意的是:
- Parfor循环应该是整数。
- 所有的矩阵必须被分类(阅读文档)。
- 容器矩阵应该用在嵌套for循环中
这是一种方法,我会这样做,虽然它取决于您的最终申请
iVal = 5:0.2: 5.4;
jVal = 2:0.5:2.5;
iLen =长度(iVal);
jLen = length(jVal);
matrx =零(艾伦,jLen);
parfor i = 1:iLen
dummy =零(1,jLen);
for j = 1:jLen
dummy(j)= iVal(i)* jVal(j);
end
matrx(i,:) = dummy;
end
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?
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:
- Parfor loops should be integers.
- All matrices must be classified (read the documentation).
- 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
这篇关于将嵌套的FOR循环转换为PARFOR循环matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!