2017-06-03 61 views
0

我想在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

回答

1

正如錯誤消息中所述,您必須遵循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嗎? –