我正在尝试优化以下MATLAB代码的时间效率,它目前需要超过4个小时才能运行(我已经预先分配了两个结构,但这里没有包含该部分):
for combination = 1:1771
for hankel_size = 1:4;
for window = 1:999
Output.bin_r(:, window, combination, hankel_size) = bsxfun(@minus, data.hankel_index_mean(window, combination ,hankel_size),centers(window, :, hankel)');
Output.score(window, combination, hankel_size) = probs(window, :, hankel_size)*Output.bin_r(:, window, combination, hankel_size);
end
end
end
请注意:
提前感谢您的帮助!
答案 0 :(得分:2)
parfor combination = 1:1771
for hankel_size = 1:4;
for window = 1:999
Output.bin_r(:, window, combination, hankel_size) = bsxfun(@minus, data.hankel_index_mean(window, combination ,hankel_size),centers(window, :, hankel)');
Output.score(window, combination, hankel_size) = probs(window, :, hankel_size)*Output.bin_r(:, window, combination, hankel_size);
end
end
end
parfor
利用CPU中的所有核心。
通过matlab默认打开并行池,默认在调用并行函数(例如parfor
或spmd
)时打开一个并通过调用parpool
或{{1 }}
在Home-> parallel-> parallel preferences下编辑并行首选项。