function [TOT_RESPONSE_MATRIX_binned,opts] = binRespMat(TOT_RESPONSE_MATRIX,AREA_LIST,opts)

if opts.binnedMat == 1
     TOT_RESPONSE_MATRIX_binned = TOT_RESPONSE_MATRIX;
elseif (opts.ampl_binn_meth > 0) || (numel(unique(TOT_RESPONSE_MATRIX)) > 8) || ( max(diff(unique(TOT_RESPONSE_MATRIX))) > 1)
    if opts.ampl_binn_meth == 0
        opts.ampl_binn_meth = 3;
    end
    for area = AREA_LIST
        TOT_RESPONSE_MATRIX_AREA = squeeze(TOT_RESPONSE_MATRIX(area,:,:));
        TOT_RESPONSE_MATRIX_AREA_ONE_COL = reshape(TOT_RESPONSE_MATRIX_AREA',[size(TOT_RESPONSE_MATRIX_AREA,1)*size(TOT_RESPONSE_MATRIX_AREA,2)],1);
        if opts.ampl_binn_meth == 1
            [TOT_RESPONSE_MATRIX_AREA_ONE_COL_binned,Ns] = equi_binning2(TOT_RESPONSE_MATRIX_AREA_ONE_COL,opts.ampl_bins_num,0);
        elseif opts.ampl_binn_meth == 2
            TOT_RESPONSE_MATRIX_AREA_ONE_COL_binned = binr(TOT_RESPONSE_MATRIX_AREA_ONE_COL', size(TOT_RESPONSE_MATRIX_AREA_ONE_COL,1), opts.ampl_bins_num, 'eqpop')' + 1;
        elseif opts.ampl_binn_meth == 3
            TOT_RESPONSE_MATRIX_AREA_ONE_COL_binned = binr(TOT_RESPONSE_MATRIX_AREA_ONE_COL', size(TOT_RESPONSE_MATRIX_AREA_ONE_COL,1), opts.ampl_bins_num, 'eqspace')' + 1;
        end
        TOT_RESPONSE_MATRIX_AREA_binned = reshape(TOT_RESPONSE_MATRIX_AREA_ONE_COL_binned,[size(TOT_RESPONSE_MATRIX_AREA,2),size(TOT_RESPONSE_MATRIX_AREA,1)])';
        TOT_RESPONSE_MATRIX_binned(area,:,:) = TOT_RESPONSE_MATRIX_AREA_binned;
    end
else
    TOT_RESPONSE_MATRIX_binned = TOT_RESPONSE_MATRIX+1;
end

end

