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

7 次查看(过去 30 天)
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?

回答(1 个)

Luca Ferro
Luca Ferro 2023-3-6
编辑:Luca Ferro 2023-3-7
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 个评论
ELISABETTA BILLOTTA
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
Luca Ferro 2023-3-7
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by