How can I improve my code by using logical indexing instead of "find" function.

3 次查看(过去 30 天)
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.

采纳的回答

Ahmet Cecen
Ahmet Cecen 2015-5-4
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.
  4 个评论
Ahmet Cecen
Ahmet Cecen 2015-5-5
Interesting, I get identical results. Any ways, it doesn't matter I would take up image analyst on that link, this is a very very roundabout way of indexing to begin with. Running it as it is if you get the answer you want is okay, unless you run this with a 1x1000000 SS vector later, you will not see the effects of that performance drop the error is referring to.
A=rand(5);
sigma=3;
A=
0.8147 0.0975 0.1576 0.1419 0.6557
0.9058 0.2785 0.9706 0.4218 0.0357
0.1270 0.5469 0.9572 0.9157 0.8491
0.9134 0.9575 0.4854 0.7922 0.9340
0.6324 0.9649 0.8003 0.9595 0.6787
find(SS==sigma)
ans =
3
A(2,find(SS==sigma))
ans =
0.9706
A(2,SS==sigma)
ans =
0.9706
Kerem Karatas
Kerem Karatas 2015-5-5
It works like how you explained. I had the same conclusion before I made a mistake by using "=" instead of "==" which confused me even more. Now reading and learning more about logical indexing I have a better understanding on addressing an element of an array.
Thank you for detailed explanation.

请先登录,再进行评论。

更多回答(1 个)

Edric Ellis
Edric Ellis 2015-5-5
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 个评论
Kerem Karatas
Kerem Karatas 2015-5-5
Thank you, when I adjusted my code it allowed parfor to be used instead of for. From what I read I gathered it is not possible to have two parallel for loops in a nested manner. That makes sense to me. But also makes me think...
Function calls are done in the inner loop and trialSize is much larger than numel(SS) therefore instead of making the outer loop parfor I changed the inner loop to parfor. Calls of the same function should be independent if I am not mistaken.
Thanks again for helping me understand how to make my code work parallel.
Edric Ellis
Edric Ellis 2015-5-5
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by