MATLAB: Get zeroes in a binned data set

zero elements

I am trying to put a large number of data set which is in as a matrix to certain bins. But I get zeroes in my binned data set and I don't know what to do. My program is a bit long. Once I print one data set I get random zeros everywhere in the data set. I will be grateful if anyone can help me.
clc;
clear all;
ncdisp('at.nc')
T = ncread('at.nc','T',[1 1], [Inf Inf], [1 1]);
year= ncread('at.nc','year',[1],[Inf], [1]);
month= ncread('at.nc','month',[1], [Inf], [1]);
alt= ncread('at.nc','altitude',[1], [Inf], [1]);
day= ncread('at.nc','day',[1],[Inf], [1]);
flag= ncread('at.nc','quality flag',[1 1], [Inf Inf], [1 1]);
for i=1:32025
x(i)=year(i);
d(i)=day(i);
m(i)=month(i);
end
for i=1:32025
if ((m(i)==1 )&& (d(i)>=0.5) && (d(i)<=15.25))
L(i)=1;
elseif d(i)>=15.26 && d(i)<=30.5 && m(i)==1
L(i)=2;
elseif (d(i)==31 && m(i)==1) || (d(i)<=14.75 && m(i)==2)
L(i)=3;
elseif (d(i)>=14.76 && m(i)==2) || (d(i)<=1.5 && m(i)==3)
L(i)=4;
elseif (d(i)>=16.5 && d(i)==31 && m(i)==3)
L(i)=5;
elseif (d(i)>=16.5 && m(i)==3) || (d(i)<=31 && m(i)==3)
L(i)=6;
elseif (d(i)==1 && m(i)==4) || (d(i)<=15.75 && m(i)==4)
L(i)=7;
elseif (d(i)>=15.75 && m(i)==4) || (d(i)==30 && m(i)==4)
L(i)=8;
elseif (d(i)>=1 && m(i)==5) || (d(i)<=16.75 && m(i)==5)
L(i)=9;
elseif (d(i)>=16.25 && m(i)==5) || (d(i)==31 && m(i)==5)
L(i)=10;
elseif (d(i)==1 && m(i)==6) || (d(i)<=15.75 && m(i)==6)
L(i)=11;
elseif (d(i)>=15.25 && m(i)==6) || (d(i)==30 && m(i)==6)
L(i)=12;
elseif (d(i)==1 && m(i)==7) || (d(i)<=16.75 && m(i)==7)
L(i)=13;
elseif (d(i)>=16.25 && m(i)==7) || (d(i)==30 && m(i)==7)
L(i)=14;
elseif (d(i)==1 && m(i)==8) || (d(i)<=15.75 && m(i)==8)
L(i)=15;
elseif (d(i)>=15.75 && m(i)==8) || (d(i)==31 && m(i)==8)
L(i)=16;
elseif (d(i)>=0.5 && m(i)==9) || (d(i)<=15.75 && m(i)==9)
L(i)=17;
elseif (d(i)==30 && m(i)==9) || (d(i)<=15.75 && m(i)==9)
L(i)=18;
elseif (d(i)==1 && m(i)==10) || (d(i)<=15.75 && m(i)==10)
L(i)=19;
elseif (d(i)<=31 && m(i)==10) || (d(i)>=15.75 && m(i)==10)
L(i)=20;
elseif (d(i)==1 && m(i)==11) || (d(i)<=15.75 && m(i)==11)
L(i)=21;
elseif (d(i)==30 && m(i)==11) || (d(i)>=15.75 && m(i)==11)
L(i)=22;
elseif (d(i)==1 && m(i)==12) || (d(i)<=15.75 && m(i)==12)
L(i)=23;
elseif (d(i)==31 && m(i)==12) || (d(i)>=15.75 && m(i)==12)
L(i)=24;
else L(i)
end
end
n=0;
for i=1:120
for m=1:24
w4(i,m)=1;
w5(i,m)=1;
w6(i,m)=1;
w7(i,m)=1;
w8(i,m)=1;
w9(i,m)=1;
w10(i,m)=1;
w11(i,m)=1;
w12(i,m)=1;
w13(i,m)=1;
end
end
for j=1:32025
if (x(j)==2004)
for i=1:120
if (flag(i,j)<=2)&& ((T(i,j))>0)&&(~isnan(T(i,j)))
E4(i,L(j),w4(i,L(j)))= T(i,j);
E4(i,L(j),w4(i,L(j)));
w4(i,L(j))= w4(i,L(j))+1;
end
end
elseif (x(j)==2005)
if (flag(i,j)<=2)&& ((T(i,j))>0)&&(~isnan(T(i,j)))
for i=1:120
E5(i,L(j),w5(i,L(j)))= T(i,j)
w5(i,L(j))= w5(i,L(j))+1;
end
end
elseif (x(j)==2006)
if(flag(i,j)<=2) && ((T(i,j))>0)&&(~isnan(T(i,j)))
for i=1:120
E6(i,L(j),w6(i,L(j)))= T(i,j);
w6(i,L(j))= w6(i,L(j))+1;
end
end
elseif (x(j)==2007)
if(flag(i,j)<=2)&& ((T(i,j))>0)&&(~isnan(T(i,j)))
for i=1:120
E7(i,L(j),w7(i,L(j)))= T(i,j);
w7(i,L(j))= w7(i,L(j))+1;
end
end
elseif (x(j)==2008)
if(flag(i,j)<=2)&& ((T(i,j))>0)&&(~isnan(T(i,j)))
for i=1:120
E8(i,L(j),w8(i,L(j)))= T(i,j);
w8(i,L(j))= w8(i,L(j))+1;
end
end
elseif (x(j)==2009)
if(flag(i,j)<=2)&& ((T(i,j))>0)&&(~isnan(T(i,j)))
for i=1:120
E9(i,L(j),w9(i,L(j)))= T(i,j);
w9(i,L(j))= w9(i,L(j))+1;
end
end
elseif (x(j)==2010)
if (flag(i,j)<=2)&& ((T(i,j))>0)&&(~isnan(T(i,j)))
for i=1:120
E10(i,L(j),w10(i,L(j)))= T(i,j);
w10(i,L(j))= w10(i,L(j))+1;
end
end
elseif (x(j)==2011)
if(flag(i,j)<=2)&& ((T(i,j))>0)&&(~isnan(T(i,j)))
for i=1:120
E11(i,L(j),w11(i,L(j)))= T(i,j);
w11(i,L(j))= w11(i,L(j))+1;
end
end
elseif (x(j)==2012)
if(flag(i,j)<=2)&& ((T(i,j))>0)&&(~isnan(T(i,j)))
for i=1:120
E12(i,L(j),w12(i,L(j)))= T(i,j);
w12(i,L(j))= w12(i,L(j))+1;
end
end
elseif (x(j)==2013)
if(flag(i,j)<=2)&& ((T(i,j))>0)&&(~isnan(T(i,j)))
for i=1:120
E13(i,L(j),w13(i,L(j)))= T(i,j);
w13(i,L(j))= w13(i,L(j))+1;
end
end
end
end
I printed E4. The results I get is attached in the question.

Best Answer

I don't really know the answer to your problem, mostly because your program is so difficult to follow with all these elseif it's difficult to see where the bug, if any, could be. Possibly, it's because some of your elseif may be wrong, such as:
elseif (d(i)>=16.5 && d(i)==31 && m(i)==3)
if d == 31, it's obviously >= 16.5, so the _d(i)>=16.5 serves no purpose
elseif (d(i)==1 && m(i)==4) || (d(i)<=15.75 && m(i)==4)
the first part (before the | |) is always true when the second is, thus serves no purpose. There may be more elseif like that.
So, I think your first task should be to simplify your program. Most of what you're doing can be achieved with a lot less code. For example:
  • copying matrix: I'm not sure why you copy year, day, month to new arrays with names which are less descriptive but you don't need a loop to do that,
x = year; % or x = year(1:32025) if year has more elements
works just as well.
Your L calculation looks like it's partitioning the year into several periods and finding in which period a particular month/day combination falls in. You're basically finding in which bin of an histogram a particular date falls in. The 2nd output of histc tells you that. You just need to transform your month/day dual variable into a single one. This is easily done with datenum and datevec_, e.g.:
dvdaymonth = [zeros(32025, 1) m' d']; %assuming m and d are row vector. Don't transpose if column
%dvdaymonth is a datevector where each row is year month day. year is always 0
dndaymonth = datenum(dvdaymonth); %transform into a single number
dvthresholds = [
0 0 0
0 1 15.25
0 1 30
0 2 14.75
... and so on
0 12 31]; %again, each row is year, month, day.
dnthresholds = datenum(dvthresholds);
[~, L] = histc(dndaymonth, dnthresholds);
You use loops to create matrices of one, use the ones function:
w4 = ones(120, 4);
...
I'm not sure what you're doing next in the code, it looks like you're building an histogram. maybe explain what you're trying to achieve and we'll tell you how to simplify it.
With simpler code, it'll be a lot easier to find where it's going wrong.
Related Question