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

1 view (last 30 days)
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!

Accepted Answer

Walter Roberson
Walter Roberson on 28 Nov 2017
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 Comments
Walter Roberson
Walter Roberson on 28 Nov 2017
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!