Vectorize multiple nested for loops

Please help a newbie. I am new to Matlab and am trying to vectorize some nested for-loops that compute distance from 3 defined measuring positions (called 'Pos_Stations') from a single signal. I am having a hard time trying to understand how the loops can compute multiple operations within these loop(s). The code is pasted below and probably does a better job of explaining my issue.
yp = [-1:0.02:7];
xp = [-5:0.02:5];
Power_Guess = [20:0.1:40];
for ll = 1:length(Power_Guess)
%%%%Guess at position
for k=1:length(xp)
for j = 1:length(yp)
% Current guess
guess = [xp(k) yp(j)];
for i=1:length(Pos_Stations)
%%%%guess is the guess position of the transmitter
%%%%Del is the distance between station "Pos_Staions" and
%%%%the "guess"
Del(i,:) = Pos_Stations(i,:)- guess;
end
temp = Del.^2;
%%%%distance from the station to the guessed position of the transmitter
Dist = sqrt(sum(temp'));
Dist_S1 = Dist(1);
Dist_S2 = Dist(2);
Dist_S3 = Dist(3);
P_guess = Power_Guess(ll);
S_prime_1 = P_guess/Dist_S1^2;
S_prime_2 = P_guess/Dist_S2^2;
S_prime_3 = P_guess/Dist_S3^2;
Error_S1 = S_prime_1 - S(1);
Error_S2 = S_prime_2 - S(2);
Error_S3 = S_prime_3 - S(3);
Std_Error(k,j) = sqrt(Error_S1^2 + Error_S2^2 + Error_S3^2 );
end
end
Inv_Error = 1./Std_Error;
Max_Inv_Error = max(max(Inv_Error));
[ii jj] = find(Inv_Error==Max_Inv_Error);
Tx_pos=[xp(ii) yp(jj)];
Stats(ll,:) = [xp(ii) yp(jj) P_guess max(max(Inv_Error))];
end

 Accepted Answer

Star Strider
Star Strider on 21 Feb 2018
I cannot understand what you are doing. If you have the Statistics and Machine Learning Toolbox, see if the pdist2 (link) function will do what you want.

6 Comments

Thank you for the response.
Mainly, I would just like to know if and how to vectorize a set of loops such as those in my code. The loops have a number of operations that make a straight conversion to vectors from the for loops a bit complicated. In shot - I want to optimize the code.
Can this be done?
My pleasure.
Do you already know the transmitter position coordinates and are computing the distance? The pdist2 function will compute those distances.
If you want to compute them yourself, this works, and gives the same result as pdist2:
Xmtr = rand(1, 2); % Transmitter Coordinates
Rcvrs = rand(3, 2); % Receiver Coordinates
Distance = sqrt(sum(bsxfun(@minus, Rcvrs, Xmtr).^2, 2)); % Distance Vector
The code is straightforward. It uses bsxfun to subtract the coordinates of ‘Xmtr’ from the coordinates of ‘Rcvrs’, squares them, sums across the columns, and takes the square root of the sums to calculate the distance. (Note that this assumes all are on a plane, and does not take into account great circle distances.)
Thanks again for the help. Yes, I know the position of transmitter, but am making guesses during the innermost for loop to collect errors. I am trying to brute force search lowest error
My pleasure.
There is no reason to guess, as I understand what you are doing. You can calculate it exactly.
If you don’t know the minimum distance, there are several functions in the Optimization Toolbox and the Global optimization Toolbox to minimise the distances. A ‘brute force’ approach is neither desirable nor computationally efficient.
As always, my pleasure.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!