在matlab中使用parfor

问题描述:

我想在MATLAB中并行化我的部分代码。例如低于部分:在matlab中使用parfor

v1=[1,3,6,8]; 
ggx=5.*ones(15,14); 
gax=ones(15,14); 
parfor i = 1:length(v1) 
m = v1(i); 
if m > 1 
gax(1:m-1,m-1) = ggx(1:m-1,m-1); 
end 
if m<nn 
gax(m+1:end,m) = ggx(m+1:end,m); 
end 
end 

但有一个错误: 错误:在PARFOR可变GAX不能成为在MATLAB,“概述”循环classified.See并行。

有谁知道我该如何删除错误?其他有用的信息是v1是一个递增的向量,它不包含任何重复的元素。

+0

你有没有尝试在'parfor'之前初始化'gax'和'gay'?在这种情况下,我也不认为'parfor'会帮助优化你的代码。你最好只使用常规的'for'循环。 – kedarps

+0

是的,我在parfor之前给了他们价值。 @kedarps –

+0

这似乎是一个切片变量索引问题,请参阅[这里](https://www.mathworks.com/help/distcomp/troubleshoot-variables-in-parfor-loops.html)。 – kedarps

正如错误消息中所述,您必须遵循Sliced Variable rulegaxgay都违反了Fixed Index ListingForm of Indexing的规则。另外,您可以将此示例A(i,20:30,end) % 20:30 not scalar作为文档中未切片变量的示例。

因此,您应该更改parfor的所有部分以获得正确的并行计算。换句话说,您必须根据循环变量设计一种可以并行化该方法的适当并行算法。

Type of First-Level Indexing — The first level of indexing is either parentheses,(), or braces, {}.

Fixed Index Listing — Within the first-level parentheses or braces, the list of indices is the same for all occurrences of a given variable.

Form of Indexing — Within the list of indices for the variable, exactly one index involves the loop variable.

Shape of Array — The array maintains a constant shape. In assigning to a sliced variable, the right side of the assignment cannot be [] or '', because these operators attempt to delete elements.

+0

“换句话说,您必须根据循环变量设计一种可以并行化该方法的适当并行算法。”问题是如何做到这一点 –

+0

我的意见是你不能在这里做! – OmG

+0

我忘了说,v1是一个增加的矢量,它没有任何重复的元素。在这种情况下,你知道如何使用parfor吗? –