Do I need nested loops?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Bob Murphy
el 17 de Mzo. de 2016
Comentada: Bob Murphy
el 22 de Mzo. de 2016
I am trying to create a small script that will allow me to expand the perimeter based off coordinates. The program is running through the variables correctly when I look at the workspace, but I can't get it to write the results to the structure multiple times. It will only write to the structure once. Am I missing a nested loop?
load 'lat_lon.mat'
new_coord = [];
for i = 1:length(lat_lon)
1
new_coord.Lat = [lat_lon(:,[1])];
new_coord.Lon = [lat_lon(i:,[2])];
2
%Finds the radius of the Earth at given latitude
radius = 6372 * cos(lat_lon(i,[1])/180 * pi)
3
%Converts given perimeter expansion to degrees
lat_nat_deg = nm2deg(3.5, radius);
new_coord.lat_exp = [lat_nat_deg];
nat_deg = nm2deg(3.5);
new_coord.lon_exp = [nat_deg];
4
new_coord.latitude = [lat_lon(i,[1]) + lat_nat_deg];
5
new_coord.longitude = [lat_lon(i,[2]) + nat_deg]
end
The lat_lon.mat is :
Lat_Long = [60.8427194, -042.7969500; ...
60.8646972, -042.5323889; ...
60.7640417, -042.5178639; ...
60.5882139, -042.8185056; ...
60.8080111, -042.8507500; ...
60.8069889, -042.8374167; ...
60.8083028, -042.8278361; ...
60.8114056, -042.8186167; ...
60.8152306, -042.8117389; ...
60.8203694, -042.8055667; ...
60.8256528, -042.8013611; ...
60.8315167, -042.7985083; ...
60.8374056, -042.797078; ...
60.8427194, -042.7969500];
0 comentarios
Respuesta aceptada
Chad Greene
el 17 de Mzo. de 2016
The loop you've written overwrites the results each time through the loop. Any information you want to log such as newcoord.latitude must be indexed to i. (Better yet, change i to k because i is the imaginary unit in Matlab and defining i as a variable can occasionally cause errors.)
Here are two ways you can organize your structure:
for k = 1:10
newcoord.lat{k} = randi(90,1,2);
anotherway(k).lat = randi(90,1,2);
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!