not entering nested for loop

So I am averaging the values of a few pixels in a an area specified by the two for loops parameters (l&m). However, I must be staring at this code for too long as it is not entering the nested loop resulting in an average of zero... which it shouldn't be.
The pixel values of 0 & 12 correlate to black and white which is why they are over-written to not be included in the image due to some of the image having text and lines on top of it...
tic
localurl='http://www.ips.gov.au/Images/HF%20Systems/Global%20HF/Ionospheric%20Map/WorldIMap0.gif';
% [stat, h, localurl]=web(localurl);%doesnt have to run this line
[mat,map]=imread(localurl);
colormap(map);%doesnt have to run this line
image(mat);%doesnt have to run this line
c=clock;
year=num2str(c(1));
month=num2str(c(2));
day=num2str(c(3));
hour=num2str(c(4));
min=num2str(c(5));
savename=[year '.' month '.' day '.' hour '.' min '.fig'];
savefig(savename)
.
.
.
s=1;
value=zeros(1,1);
for l=178:184
disp('1st')
for i=224:218
disp('2nd')
value(s)=mat(m,l);
if value(s)~=0 && value(s)~=12
s=s+1;
end
end
end
avg=mean(value(:));
toc

 Accepted Answer

Jan
Jan on 30 Jun 2014
Edited: Jan on 30 Jun 2014
  • Your inner loop does not depend on "i", but there is an orphaned "m" without a definition.
  • The inner loop goes fro 224 to 218, but the first index is larger than the second one.
I guess your loop code can be simplified:
data = mat(218:224, 178:184);
avg = mean(data(data ~= 0 & data ~= 12))

1 Comment

thank you so much, and ya i is m I had been messing with some values and forgot to change it back!

Sign in to comment.

More Answers (1)

s=1;
value=0;
for l=178:184
for i=224:218
value(s)=mat(m,l);
if value(s)~=0 && value(s)~=12
s=s+1;
end
end
end
avg=mean(value(:));
value(s)=mat(m,l);
What's m??? It's never update in the loop so you're always looking at the same pixel. Or, looking again I suppose you meant to write
for m=224:218
instead of i for the inner loop.
The above could be simply
c1=178; c2=184;
r1=224; r2=218;
avg=mean(mat(mat(r1:r2,c1:c2)~=0 & mat(r1:r2,c1:c2)~=12));

Categories

Find more on Images in Help Center and File Exchange

Asked:

on 30 Jun 2014

Commented:

on 30 Jun 2014

Community Treasure Hunt

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

Start Hunting!