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!
0 个评论
采纳的回答
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
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 Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!