Fill structure in parallel loop

9 ビュー (過去 30 日間)
soepblik
soepblik 2021 年 9 月 9 日
回答済み: Edric Ellis 2021 年 9 月 10 日
I have the following structure:
The company structure consist out of 3 fields
  • old
  • new
  • feature
these 3 fields are also structures and consists out 11 fields
These 11 fields have dimensions of 45*60.
There are 45 companies. So each company is filling a line in each field from the old,new,feature.
These fields are filled now as follows:
for i = 1:amount
company = FillFields(company)
end
If i make it a parfor loop then this construction will not work.
But i don't now how to easily make it work with a parfor loop?
Thanks in advance!

回答 (1 件)

Edric Ellis
Edric Ellis 2021 年 9 月 10 日
The main constraint here is the parfor "sliced output" requirement. Basically you cannot directly update a single struct using parfor, but you can use parfor to assemble the fields separately, and then put them together into a struct afterwards. For example:
parfor i = 1:7
name{i} = sprintf('name %d', i);
value(i) = randi(10);
end
result = struct('name', {name}, 'value', {value})
result = struct with fields:
name: {'name 1' 'name 2' 'name 3' 'name 4' 'name 5' 'name 6' 'name 7'} value: [6 4 7 2 5 10 3]

カテゴリ

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