MATLAB中的单元数组使用parfor

时间:2015-06-19 22:39:10

标签: matlab parfor

我需要在MATLAB中并行化脚本。我有一个我要返回值的单元格数组。但是,MATLAB不接受我构建脚本并行化的方式。

_dbContext.Nodes
     .Where(n => n.Tags.Count(t => tags.Contains(t.DisplayName)) == tags.Count)
     .Select(...

MATLAB回应变量N_h = 4; N_r = 6; N_s = 20; P{1:N_h, 1} = zeros(N_s, N_r); workers = 4; % number of cores (workers) for parallel computing multicore = parpool('local', workers); % open multiple workers (cores) for parallel computation for h = 1:1:N_h for r = 1:1:N_r parfor s = 1:N_s P{h,1}(s,r) = some function ... end end end delete(multicore); % delete multiple workers (cores) opened for parallel computation 的索引方式与P不兼容。我该如何更改脚本?

1 个答案:

答案 0 :(得分:1)

最简单的方法是创建一个临时向量,将并行结果存储在那里,然后一次性分配所有值。

for h = 1:1:N_h
    for r = 1:1:N_r
        svec = zeros(N_s, 1);
        parfor s = 1:N_s
            svec(s) = my_very_parallelizable_func(param1, param2);
        end
        P{h,1}(:,r) = svec;
    end
end
相关问题