How can I improve my code by using logical indexing instead of "find" function.
Show older comments
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.
Accepted Answer
More Answers (1)
Edric Ellis
on 5 May 2015
I would try something along these lines:
parfor sidx = 1:numel(SS)
sigma = SS(sidx);
for i = 1:trialSize
estErr(i, sidx) = ...
end
end
The trick here is to make the loop variable operate over the number of elements of SS, and then deduce sigma from that.
2 Comments
Kerem Karatas
on 5 May 2015
Edric Ellis
on 5 May 2015
It's usually best to make the outer loop the parfor, but if you don't have enough parallelism there, then it makes sense to make the inner loop the parfor. Basically it comes down to balancing the amount of time wasted when there isn't enough parallelism (when some workers are idle) vs. the overhead of launching a parfor loop.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!