试图将以下内容用于时间效率MATLAB

时间:2015-08-19 09:35:44

标签: matlab

我正在尝试优化以下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

请注意:

  • center是一个999 x 50 x 4矩阵
  • hankel_index_mean是一个999 x 1771 x 4矩阵
  • probs是一个999 x 50 x 4矩阵

提前感谢您的帮助!

1 个答案:

答案 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默认打开并行池,默认在调用并行函数(例如parforspmd)时打开一个并通过调用parpool或{{1 }}

在Home-> parallel-> parallel preferences下编辑并行首选项。