This may be what you want, but your description of the problem is very hard to interpret. You just want the number of non-zero elements in each column of true_spike?
[dateAndHour,~,index] = unique(data(:,1:4),'rows'); % find the hours present in the matrix and attribute each element of the matrix to one of these hours
hourlyMean = accumarray(index,data(:,7),[],@mean); % compute the hourly mean
output = [dateAndHour,zeros(size(dateAndHour,1),2),hourlyMean]; % build the output
Best Answer