Parallel Computing Question for For loop switch to parfor

R
Remingtonsmith · Jan 15, 2021 · 1.8K views
Question
I am trying to find the nearest phase value to time intervals. This is the loop I have used. The  matrix is large and I want to use parallel computing function 'parfor', but cannot due to having embedded functions within a function. I somewhat understand that they need things to be as simplistic as possible, but is there a way to fix this so that I can use 'parfor' function? I do not fully understand why the parallel processing has the limitations, so I am not exactly sure how to fix it. Any help would be greatly appreciated!   for i=1:length(timeintervals(:,1)) for ii=1:length(timeintervals) [~,ix]=min(abs(timeintervals(i,ii)-phasevalues(:,2))); % find nearest location out(i,ii)=phasevalues(ix); end end
Expert Answer
Profile picture of Neeta Dsouza
Neeta Dsouza PhD Expert
Answered Aug 25, 2026
Why not as follows?
 
 
 col1=phasevalues(:,1);
 col2=phasevalues(:,2); %as separate variable, repeated memory allocation is avoided

 parfor j=1:size(timeintervals,2)
   [~,ix]=min( abs(timeintervals(:,j).' - col2 ) );  % find nearest location
   out(:,j)=col1(ix);   
 end
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!