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

조회 수: 2 (최근 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에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by