matlab - changing the size of stencil during iterations within a for loop -
i have matrix [35rows x 39columns] of 2x1 column vectors, representing ordered pairs. have function creates 9 point stencil , evaluates points, returns 9 values , picks largest one. creates 9 point stencil centered largest return value located, , continues until function terminates @ global maximum value in matrix.
my question is, how can make stencil more efficient disregarding points has evaluated? don't want evaluate 9 points every time because take longer, , trying make process fast possible.
process: 1.) evaluate point , 8 surrounding it, 2.) pick largest of return values, 3.) move point, 4.) repeat
i referencing cell, use {i,j} indexing.
here's have code:
for k = 1:10^(6) setpoint1 = field{i,j}(1); setpoint1 = field{i,j}(2); setpoint2 = field{i-1,j-1}(1); setpoint2 = field{i-1,j-1}(2); setpoint3 = field{i-1,j}(1); setpoint3 = field{i-1,j}(2); setpoint4 = field{i-1,j+1}(1); setpoint4 = field{i-1,j+1}(2); setpoint5 = field{i,j-1}(1); setpoint5 = field{i,j-1}(2); setpoint6 = field{i,j+1}(1); setpoint6 = field{i,j+1}(2); setpoint7 = field{i+1,j-1}(1); setpoint7 = field{i+1,j-1}(2); setpoint8 = field{i+1,j}(1); setpoint8 = field{i+1,j}(2); setpoint9 = field{i+1,j+1}(1); setpoint9 = field{i+1,j+1}(2);
from there make 9 function calls, , record rsquared value (temp1 etc object multiple fields, concerned rsquared.)
tempvec = [temp1.rsquared,temp2.rsquared,temp3.rsquared,temp4.rsquared,temp5.rsquared,temp6.rsquared,temp7.rsquared,temp8.rsquared,temp9.rsquared]; nextcenter = find(tempvec==max(tempvec)); if nextcenter == 1 break; end if nextcenter == 2; = i-1; j = j-1; elseif nextcenter == 3; = i-1; elseif nextcenter == 4; = i-1; j = j+1; elseif nextcenter == 5; j = j-1; elseif nextcenter == 6; j = j+1; elseif nextcenter == 7; = i+1; j = j-1; elseif nextcenter == 8; = i+1; elseif nextcenter == 9; = i+1; j = j+1; end nextcenter = []; end
and dan, found out when comparing different algorithm :( can fix that, need stencil.
if there no more information available properties of function (monotonicity nice) there not better trying possibilities.
at least make sure once, guess trivial if go through of them.
if want performance, try cellfun
little more complicated loop can give best speed.
if still looking way make heuristic more efficient, track progress:
tobeevaluated = true(35,39);
if consider evaluate point, check whether still done. after evaluating set value in progress matrix false.
Comments
Post a Comment