Azzera filtri
Azzera filtri

repeating the same statement using loop

1 visualizzazione (ultimi 30 giorni)
Mayank Lakhani
Mayank Lakhani il 16 Feb 2016
Modificato: dpb il 16 Feb 2016
Hi all,
I am doing same proces for the three time for the range, doppler and angle information. How to do it just by using one time. I want to use only one loop where i can get Result_R, Result_V, Result_A.
%%%for range information
G2 = f_range_m(1:8:length(timeline_ms),:); %%%%measured range
G1 = OrgiRange(1:8:end,:);
obj = 10;
Result=nan(size(G1,1),obj);
for ix = 1:obj
M = G1(:,ix);
dist = abs(bsxfun(@minus,G2,M));
[~,col] = min (dist,[],2);
Result(:,ix) = diag(G2(:,col));
end
%%%For doppler information
G22 = f_vel_mps(1:8:length(timeline_ms),:); %%%%measured range
G11 = xVelOrgins_g(1:8:end,:);
obj = 10;
Result_V=nan(size(G11,1),obj);
for ix = 1:obj
M = G11(:,ix);
dist = abs(bsxfun(@minus,G22,M));
[~,col] = min (dist,[],2);
Result_V(:,ix) = diag(G22(:,col));
end
%%%for angle information
G222 = s_angQ15(1:8:length(timeline_ms),:); %%%%measured range
G111 = OrgiAngle(1:8:end,:);
obj = 10;
Result_A=nan(size(G111,1),obj);
for ix = 1:obj
M = G111(:,ix);
dist = abs(bsxfun(@minus,G222,M));
[~,col] = min (dist,[],2);
Result_A(:,ix) = diag(G222(:,col));
end

Risposta accettata

dpb
dpb il 16 Feb 2016
Modificato: dpb il 16 Feb 2016
Refactor out the commonality into a subroutine where can use the same variable names without clashing with scope --
obj = 10; % global parameter
% range
G2 = f_range_m(1:8:length(timeline_ms),:);
G1 = OrgiRange(1:8:end,:);
Result_R=infoFunc(G1,G2,obj);
% doppler
G2 = f_vel_mps(1:8:length(timeline_ms),:);
G1 = xVelOrgins_g(1:8:end,:);
Result_V=infoFunc(G1,G2,obj);
% angle
G2 = s_angQ15(1:8:length(timeline_ms),:);
G1 = OrgiAngle(1:8:end,:);
Result_A=infoFunc(G1,G2,obj);
function res=infoFunc(G1,G2,obj)
res=nan(size(G1,1),obj);
for ix = 1:obj
dist = abs(bsxfun(@minus,G2,G1(:,ix)));
[~,col] = min(dist,[],2);
res(:,ix) = diag(G2(:,col));
end
Rename the function infoFunc to whatever makes sense and put it in a file of that name on the MATLABPATH so it can be found. Or, if the other code is in a higher-level function rather than a script, it could be a private function within it. Unless the data were to be stored in a different structure, replacing the outer logic with a loop would create more issues than it would solve.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by