简体   繁体   中英

Trying to Vectorise the following for time efficiency MATLAB

I am trying to optimize the time efficiency of the following MATLAB code, it currently takes in excess of 4 hours to run (I have preallocated the two structures just not included that part here):

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

Note that:

  • centers is a 999 x 50 x 4 matrix
  • hankel_index_mean is a 999 x 1771 x 4 matrix
  • probs is a 999 x 50 x 4 matrix

Thanks for your help in advance!

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 utilises all the cores in your CPU.

Open a parallel pool by either the matlab default which opens one on the calling of parallel functions (eg parfor or spmd ) or open one explicitly by calling parpool or gcp .

Edit parallel preferences under Home->parallel->parallel preferences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM