Vectorize multiple nested for loops
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Richard Haney
el 21 de Feb. de 2018
Comentada: Star Strider
el 22 de Feb. de 2018
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
0 comentarios
Respuesta aceptada
Star Strider
el 21 de Feb. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Monte-Carlo en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!