在 matlab 中使用 parfor

using parfor in matlab

我想在 matlab 中并行化我的部分代码。例如下面的部分:

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 不能 classified.See MATLAB 中的并行 for 循环,"Overview".

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

如错误信息中所述,您必须遵循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.