# how to find the value of an index in a for loop

4 vues (au cours des 30 derniers jours)
ELISABETTA BILLOTTA le 6 Mar 2023
Modifié(e) : Luca Ferro le 7 Mar 2023
lonP = 14.3;
latP = 40.4;
limlon=[min(longrd),max(longrd)];
limlat=[min(latgrd),max(latgrd)];
ic=0;
for i=1:length(longrd)
for j=1:length(latgrd)
ic=ic+1;
lonlatgrd(ic,1)=longrd(i);
lonlatgrd(ic,2)=latgrd(j);
end
end
grid_ang = calcola_ang_jacopo1(lonlatgrd,[lonP,latP]); %angles
for i2=1:length(grid_ang)
angsel=grid_ang(i2)*180/pi;
end
with this script and this function that I have attached, within this grid I have to try to identify the values of the i2 index in the different points of the grid. I have to try to understand how the values vary within the grid. Can anyone help me?
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponses (1)

Luca Ferro le 6 Mar 2023
Modifié(e) : Luca Ferro le 7 Mar 2023
if you just want to see how it varies numerically, print it:
for i2=1:length(grid_ang)
angsel=grid_ang(i2)*180/pi;
sprintf('%d ',i2) %EDIT: correcting a typo, see comments fro more
end
if you want to see how it varies graphically, plot it:
for i2=1:length(grid_ang)
angsel=grid_ang(i2)*180/pi;
id2_idx(i2)=i2; %store in in an array
end
plot(id2_idx,grid_ang) %plot it against the grid
##### 2 commentairesAfficher AucuneMasquer Aucune
ELISABETTA BILLOTTA le 6 Mar 2023
it does not work.
in the first case it gives me the error "Unrecognized function or variable 'i_idx'."
and in the second case "Error using plot. Vectors must be the same length."
Luca Ferro le 7 Mar 2023
in the first one there is a typo, im sorry:
for i2=1:length(grid_ang)
angsel=grid_ang(i2)*180/pi;
sprintf('%d ',i2) %here is the typo, instead of i2_idx is just i2
end
The second one works on my machine, try to put a breakpoint on the plot line and check the dimensions of id2_idx and grid_ang and share them here

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!