MATLAB: Alter a matrix

matrix

I have a matrix ('data') which is composed of day of year in the first column and temperature data in the second column and also a vector ('dDates') of values which also represent day of year. e.g.
clear all
n = 366;
day = linspace(1+1/24,n,(n-1)*24)';
temp = rand(8760,1);
data = [day,temp];
dDates = [12, 32, 45, 67];
I'm trying to alter 'data' so that it only contains data which is measured for the 120 rows following the day number specified in 'dDates' although being the same size as the original 'data' i.e. the other rows filled with nans.
Each measurement in 'data' refers to one hour so 120 refers to 5 days worth of data.
So far I have used:
[r,c,v] = find(data(:,1)>dDates(1),1,'first');
inside a loop to find the row number of each element of 'dDates' in 'dates' but am finding it difficult to bring everything together to produce the outcome that I need. How should I go about doing this?

Best Answer

Try this:
[~,bin]=histc(dDates,day);
idx=false(size(data,1),1);
for k=1:length(bin)
idx(bin(k)+1:bin(k)+120)=1;
end
result=NaN(size(data));
result(idx,:)=data(idx,:);
I took "... data ... following the day number..." to mean that you don't want the row that matches dDates in the result. If you do, change bin+1:bin+120 to bin:bin+119 in the above.