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

data sortfindparallel computingParallel Computing Toolboxparfor

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 600×300 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!

Best Answer

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.