How can I improve my code by using logical indexing instead of "find" function.
조회 수: 4 (최근 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
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
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
추가 답변 (1개)
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
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 Center 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!