function emissionMatrix = HelperUpdateEmissionMatrix(data,range,emissionMatrix)
% Copyright 2018 The MathWorks, Inc.

if isempty(emissionMatrix)
    emissionMatrix = zeros(range(2) - range(1) + 1);
end

data(1:100) = 0;

%% Condense regions of zeros to single zeros
index = ones(numel(data),1,'logical');
for i = 1:numel(data)-1
    if data(i)==0 && data(i+1)==0
        index(i) = false;
    end
end
data1 = data(index);

%% Round all values
data1 = round(data1);

%% Saturate data outside the range (except 0)
for i = 1:numel(data1)
    if data1(i) ~= 0
        if data1(i) > range(2)
            data1(i) = range(2);
        end
        if data1(i) < range(1)
            data(i) = range(1);
        end
    end
end

%% Cycle through data and update emission array.
for i = 2:numel(data1)
    if data1(i) ~=0
        if data1(i-1) ~= 0
            row = data1(i) - range(1);    % Observation
            col = data1(i-1) - range(1);  % Previous Observation
            
            emissionMatrix(row,col) = emissionMatrix(row,col) + 1;
        end
    end
end
