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

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

Jan
Jan 2021년 1월 26일
편집: Jan 2021년 1월 26일
I've formatted the code to improve the readability.
Please post some test data, e.g. created by rand(). Otherwise we cannot run your code to see, what happens.
Notes:
nnz(IM) % nicer and faster than: length(find(nonzeros(IM)))
The expression "all(pixl_list==0)" is dangerous, if it is not clear if the argument is a matrix or a vector. It is safer to specify the dimension to operate on: all(pixl_list==0, 1)
Hello,
thank you for your quick answer. I can't provide the original matrix because I extracted them from an image. But matrix with random values work for running the code. For example we can use fi= randi([1 10], 2000,2992) and IM= randi([-500 500], 2000,2992). The most important thing is to be able to fill "pixel_list " with the 5984000 values contained in "IM" (2000*2992= 5984000 ).
Thank you for your help!
Sorry I noticed I made a mistake: "fi" should contain values between -pi and pi so its better if we assume fi= round(randi([-3, 3], 2000,2992)/2+1.2, 1) to run the code.
Bests
Please set a breakpoint on the line containing that break statement and show us the output of the following command executed when MATLAB stops at that breakpoint:
whos cols_withonly0 pixl_list
Hello, I obtained this:
Name Size Bytes Class Attributes
cols_withonly0 1x0 0 double
pixl_list 63x232552 14650776 uint8
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.
ok thank you!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2021년 1월 26일

댓글:

2021년 1월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by