Can you do a parfor if Parallel Toobox is present else do a for

2 ビュー (過去 30 日間)
John Fox
John Fox 2017 年 6 月 29 日
コメント済み: Walter Roberson 2017 年 6 月 30 日
Is there a way: if the Parallel Toolbox is present then do the following loop with a parfor else use a for
I realize that ~isempty(which('parfor')) is true if the Parallel toolbox is present. I want to do:
if(~isempty(which('parfor')))
parfor j = 1:Lz
else
for j = 1:Lz
end
Next come the body of the loop.
Also I would like to do:
if(Lx <= 30000) % Small enough don't need parallel
for j = 1:Lz
else
parfor j = 1:Lz
end
Next comes the body of the loop.
Matlab complains "Illegal use of reserved keyword "else"."

採用された回答

Walter Roberson
Walter Roberson 2017 年 6 月 29 日
You do not need to do this. If you do not have the Parallel Computing Toolbox, parfor is automatically processed as a for loop.
  2 件のコメント
John Fox
John Fox 2017 年 6 月 30 日
The answer was:
if(Lx <= 30000) % Small enough don't need parallel for j = 1:Lz % do what you want end else parfor j = 1:Lz % do what you want end end
I already figured out that this would work. However, both "do what you want" are the same 2 pages of code. I was hoping to avoid having two copies of two pages of code.
This seems like such an obvious issue. Is there no elegant solution other than just repeating lots of code?
Walter Roberson
Walter Roberson 2017 年 6 月 30 日
Put the code into a function. Call it in both places

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by