Fixing a nested for loop to use parallel processing to speed up computation

1 次查看(过去 30 天)
I am trying to find values in 'phasetimematrix' that are phase values. The second part of that same matrix is time stamps. Another matrix of times (called 'times') that increase by .001 in the rows and are always 300 in size. The number of rows can vary and can be as large as 600. The goal is to find the phase value for every time variable in 'times'.I am trying to implement parallel processing into my code because finding corresponding values is taking a long time for 600x300 matrices full of times that I need to find corresponding phase from "phasetimematrix". I would like to use 'parfor' but not sure how to get around the nested function.
Here is my code:
column1=phasetimematrix(:,1);
column2=phasetimematrix(:,2);
for k=1:size(times,1);
for j=1:size(times,2)
[~,ix]=min(abs(times(k,j).' - column2 ) );
output(k,j)=column1(ix);
end
end
I have attached some example matrices. Any help or advice would be greatly appreciated!

采纳的回答

Walter Roberson
Walter Roberson 2017-11-28
When I see code like
[~,ix]=min(abs(times(k,j).' - column2 ) );
I say to myself "interp1() with 'nearest' option."
As in,
ix = interp1(column2, 1:length(column2), times, 'nearest');
This presumes column2 is sorted.
  3 个评论
Walter Roberson
Walter Roberson 2017-11-28
Correct. The ix will be the index of the times to the closest entry in column2. It will be the same shape as times -- all of the lookup work is being done in a single call. You could then probably
output = column1(ix);
to do all of the extraction.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by