Problem with "if ~isempty" and "break" in a while loop

11 visualizaciones (últimos 30 días)
Martina Clairand
Martina Clairand el 26 de En. de 2021
Comentada: Martina Clairand el 27 de En. de 2021
Hello I'm running the following code but it seems that in the lines
if ~isempty(cols_withonly0)
col_0=cols_withonly0(1);
else
break
end
the condition ~isempty(cols_withonly0) is not recognized and the "break" in the while loop occurs before filling the pixl_list matrix with non zero values. "fi" and "IM" are 2000*2992 matrix with non zero values. When I run the code without the lines "if ~isempty(cols_withonly0)", "else", "break", "end" the "pixl_list" is filled as I want but I have an error message at the end because "cols_withonly0" is empty and cols_withonly0(1) exceeds array bounds. Do you have an idea where the mistake is and how to fix it to fill the "pixl_list" properly?
Thank you in advance for your answers!
pi_min=round(-pi,1);
pi_max=round(pi,1);
Non0_IM = length(find(nonzeros(IM)));
Non0_pxllist= 0;
N_max=Nx*Ny;
while Non0_IM > Non0_pxllist && pi_min < pi_max
for k=pi_min:0.1:pi_max
pos=(k+round(pi,1))*10+1;
pos=round(pos);
posi(pos)=pos;
ka(pos)=k;
[fila columna]=find(fi==k);
for ind=1: length(fila)
fifi=fila(ind);
coco=columna(ind);
pixl=IM(fifi, coco);
pixl_list (pos,ind)= pixl;
end
end
Non0_pxllist= length(find(nonzeros(pixl_list)));
cols_withonly0=find(all(pixl_list==0));
if ~isempty(cols_withonly0)
col_0=cols_withonly0(1);
else
break
end
pi_min=ka(col_0);
pi_max=pi_min;
end
  7 comentarios
dpb
dpb el 26 de En. de 2021
Certainly doesn't seem to be an issue there; it would be beneficial to see a run and the complete error message in context as well.
Just from a stylistic point, the code would be easier to read if it were written as
if isempty(cols_withonly0)
break
else
col_0=cols_withonly0(1);
end
Although as posted the irregularity in the indenting makes the code hard to read; it would seem the above could also be simplified to just
...
if isempty(cols_withonly0), break; end
col_0=cols_withonly0(1);
...
since the remainder of the loop isn't executed anyway.
Martina Clairand
Martina Clairand el 27 de En. de 2021
ok thank you!

Iniciar sesión para comentar.

Respuestas (0)

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!

Translated by