MATLAB: How to improve the code by using logical indexing instead of “find” function.

findfor looplogical indexingparfor

TLDR; I want to improve the code given below;
SS(1) = 0;
SS(2) = 0.5;
SS(3) = 1;
SS(4) = 1.5;
SS(5) = 2;
SS(6) = 2.5;
for sigma=SS
for i=1:trialSize
estErr(i,find(SS==sigma))...
= Trilateration( ROI, gridSize, N_s, P_T, P_E, sigma, alpha_actual ...
, alpha_assumed, recSens ...
, dispON ...
, useTime, assignS, assignE);
end
disp(['Shadow Spread ', num2str(SS(k)), ' is complete.']);
end
Detailed Background information; I have encountered a warning but couldn't think of a way to improving my code in the way suggested by the warning. The warning suggest that I use logical indexing instead of using find function. I am not allowed to use parfor instead of for loops here due to the way I use estErr in the code. Being able to parallelize my simulation will be a huge bonus for me. This way simulations take a long time probably due to the way I compute each estimated error obtained by each run of the function.
I am sure the loop given here and the totality of my code can be improved by adding basic coding practices. I learned MATLAB on my own, and I don't know how to "properly" write code. I wrote the code to implement localization techniques. I am trying to see how the technique fares in a simulation with changing Shadow Spread values. Sorry if my code makes you cringe. Any kind of improvement suggestion is welcome.

Best Answer

  • There are many many ways to improve that code, but the specific error you are talking about refers to:
    estErr(i,find(SS==sigma))
    and it is telling you to use:
    estErr(i,SS==sigma)
    instead.