A way to optimize this piece of code..
Show older comments
I have a switch..case inside a nested 'for-loop'. This makes the program very slow, as the for-loops themselves run for a number of iterations. Can someone suggest me a way to optimize this piece of code?
for iter = 1 : 10
for i = 1 : 10
for j = 1 : 5
if (H(j,i) == 1)
tempr = [tempr, newbitvalues(j,i)];
end
end
nZeros = nnz(~tempr);
nOnes = nnz(tempr);
switch recdmsg(i)
case 1
if nZeros > nOnes
recdmsg(i) = ~recdmsg(i);
end
case 0
if nOnes > nZeros
recdmsg(i) = ~recdmsg(i);
end
end
tempr = [];
end
end
2 Comments
It would be easier if you explained what you are trying to achieve and provided test data/arrays. I assume that the outer loop is for multiplying the run time by 10 for measuring the performance, but in the rest of the code many operations make little sense (to me).
Vijay
on 1 Oct 2017
Accepted Answer
More Answers (0)
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!