How to make this loop faster?
Show older comments
Hello,
The following code takes 30 seconds to execute. I can't make it any faster.
Any suggestion?
Thanks!
function [ at] = obstacleAttenuation( Ant, Movel, Obstaculo,mapa )
tic
fprintf('\nInside obstacleAttenuation function!\n');
at = zeros(mapa.xlength, mapa.ylength);
for xi=1:mapa.xlength
for yi=1:mapa.ylength
att = 0;
[x, y] = bresenham(Ant.Antena_x, Ant.Antena_y, Movel.x(xi), Movel.y(yi));
for i = 1:length(x)
Obs = Obstaculo(x(i), y(i));
att = att + Obs;
end
at(xi, yi) = att;
% Obs = Obstaculo(x(1:length(x)), y(1:length(x)));
% att = sum(sum(Obs));
% at(xi, yi) = att;
end
end
fprintf('\nExit obstacleAttenuation function!\n');
toc
end
NOTE: I am using R2016a version.
Answers (1)
Jesús Zambrano
on 27 Dec 2020
Is it possible to enter vectors as arguments in the function Obstaculo and get a vector as result? If so, then you could try replacing:
for i = 1:length(x)
Obs = Obstaculo(x(i), y(i));
att = att + Obs;
end
By:
i=1:length(x);
att = sum(Obstaculo(x(i), y(i)));
12 Comments
Oliver Lestrange
on 27 Dec 2020
Jesús Zambrano
on 27 Dec 2020
Edited: Jesús Zambrano
on 27 Dec 2020
Apparently, Obstaculo returns a scalar when you say
Obstaculo(x(i), y(i));
and you sum them all so to get a new scalar to save in at(xi,yi), correct?
Then, make sure that when you execute
i=1:length(x);
att = sum(Obstaculo(x(i), y(i)));
you should get a row vector of partial results (first for i=1, then for i=2,...etc.). (Please, check this).
Then, when doing
sum(Obstaculo(x(i), y(i)))
you should get a new scalar again.
Oliver Lestrange
on 28 Dec 2020
Jesús Zambrano
on 28 Dec 2020
I see. Is it solved now? Always make sure of the dimensions each function returns when using them in your code.
Oliver Lestrange
on 28 Dec 2020
Jesús Zambrano
on 28 Dec 2020
Edited: Jesús Zambrano
on 28 Dec 2020
OK, I´m not familiar with the process you are modeling, but if after executing
i=1:length(x);
att = sum(Obstaculo(x(i), y(i)));
you get att as a vector, then you have to do:
att = sum(Obstaculo(x(i), y(i)),'all');
because you need to save a scalar in each position of the matrix 'at' later on.
Jesús Zambrano
on 28 Dec 2020
Edited: Jesús Zambrano
on 28 Dec 2020
I suggest to upload the functions and dataset if the issue persists, so to clearly find out where the problem is.
Oliver Lestrange
on 28 Dec 2020
Jesús Zambrano
on 28 Dec 2020
Edited: Jesús Zambrano
on 28 Dec 2020
Oh!, you are right, that option starts in 2018b, then go with
i=1:length(x);
att = sum(sum(Obstaculo(x(i), y(i))));
you said before that when trying it, it didn't stop running. Try with few points and see that everyything is working fine. Then, run it with more points, or include breakpoints so to debug it.
Make sure Obstaculo function is handling the incoming vectors correctly. If you have multiplications, divisions or powers in that function, they must be evecuted element-by-element, so it has to include dot (.) notation: .* or ./ or .^
If initially the function gives you a single value when executing Obstaculo(x(i), y(i)) every time in the i for-loop, it should gives you a vector if you remove the for-loop and gives i=1:length(x).
Oliver Lestrange
on 28 Dec 2020
Oliver Lestrange
on 28 Dec 2020
Jesús Zambrano
on 28 Dec 2020
you're welcome!
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!