Alternative way for nested for loops and if statements
Show older comments
Hi everyone,
I am looking an alternative way for the algorithm that is mentioned below. There are nested for loops and multiple if statements.
I would like to run it faster, do you have any opinion how to do that ?
Thanks in advance.
for i = 1: totalCount
for j= 1: totalCount2
if (telArray(i,1) ~= telArray(j,1))
firstTel = find ( telArray(i,1) == sortedArray(:,3) ,1 );
[r] = find(heuristic(:,firstTel)~= 0);
placeOfFirst = r(1,1);
secondTel = find ( telArray(j,1) == sortedArray(:,3) ,1 );
[r] = find(heuristic(:,secondTel )~= 0);
placeOfSecond = r(1,1);
idx = placeOfFirst:repRate:macroPeriod;
sum1= sum(basePeriod(idx,2));
idx = placeOfSecond:repRate:macroPeriod;
sum2= sum(basePeriod(idx,2));
if( ( sum1> sum2+ 10 ) && (sortedArray (firstTel,1) > (sortedArray (secondTel , 1) + 1) ) )
firstDifference = sum1- sum2
sum1= sum1 + (macroPeriod / repRate) * ( sortedArray (secondTel ,1) - sortedArray (firstTel,1) );
sum2= sum2- (macroPeriod / repRate) * ( sortedArray (secondTel ,1) - sortedArray (firstTel,1) );
secondDifference = abs(sum1 - sum2);
if ( secondDifference < (firstDifference - 5) )
x = firstBP:repRate:macroPeriod;
basePeriod(x, 2) = basePeriod(x, 2) + sortedArray (secondTel ,1) - sortedArray (firstTel,1);
y = secondBP:repRate:macroPeriod;
basePeriod(y, 2) = basePeriod(y, 2) + sortedArray (firstTel,1) - sortedArray (secondTel ,1);
end
end
end
end
end
5 Comments
Walter Roberson
on 3 Dec 2019
Minor optimization:
[r] = find(heuristic(:,firstTel)~= 0);
placeOfFirst = r(1,1);
since you never use the other values of r, you can modify this to
placeOfFirst = find(heuristic(:,firstTel), 1);
cglr
on 3 Dec 2019
Walter Roberson
on 3 Dec 2019
I think you just might be able to create... Let me see... 4? 6? dimensional arrays that you compare everything to everything and then zero out the parts you do not need by using parallel index arrays and logical operations and operations along just the right axes...
Let me see... size(telArray,1) x size(telArray,1) x size(sortedArray,1) x repRate ... at least.
Estimated amount of the calculations that would be thrown away... Ummm, 98% or so.
Many algorithms can be converted from for loops into multidimensional vectorized problems, if you have enough memory, and are willing to wait the extra time to do the calculations that you are just going to throw away afterwards. Sometimes it can be worth doing, if the extended multidimensional problem fits into memory on a GPU, so that the extra calculations that will be thrown away are calculated in massively parallel form.
But more of the time, leaving things in loop form is
- less memory
- much much clearer code
- faster
Steven Lord
on 3 Dec 2019
Taking a step back, can you explain in words not code the goal of this script? What is it supposed to do? There may be a function or a combination of a few functions that can achieve your goal more efficiently and/or be clearer to understand.
cglr
on 4 Dec 2019
Answers (1)
J. Alex Lee
on 3 Dec 2019
For nested loops you can keep as much outside of the inner loop as possible, in this case the search for placeOfFirst looks like it can be outside the inner loop?
At first glance it doesn't seem you can really get away from the nested structure, though maybe there is...
But it also looks like you may be spending a lot of time on redundant "find()" statements. It looks like you could pre-find the indices rather than find-ing each loop (for the inner find)
Not really an optimization, but to avoid nested if's this case, you can test for opposite of what you want and continue, since you don't have other bits of code happening after the if's
for i = size(telArray,1):-1:1
idx = find ( telArray(i,1) == sortedArray(:,3) ,1 );
placeof_a(i,1) = find(heuristic(:,idx)~=0,1);
end
for i = 1: totalCount
for j= 1: totalCount2
if telArray(i,1) == telArray(j,1)
continue
end
placeOfFirst = placeof_a(i);
placeOfSecond = placeof_a(j);
% ...
end
end
Does this work?
Categories
Find more on Data Type Conversion 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!