You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: probability_maps_to_region_index.m
    path of file: ./repos/CanlabCore/CanlabCore/@atlas
    
    the code of the file until where you have to start completion: function dat = probability_maps_to_region_index(dat)
% Use dat.probability_maps to rebuild integer vector of index labels (dat.dat)
%
% dat = probability_maps_to_region_index(dat)
%

    
function dat = probability_maps_to_region_index(dat)
% Use dat.probability_maps to rebuild integer vector of index labels (dat.dat)
%
% dat = probability_maps_to_region_index(dat)
%
% Note: this script should not invoke remove_empty, replace_empty or
% resampling because it is invoked on atlas objects that may have
% mismatched probability_maps and dat fields, causing such functions to 
% fail.

% Start: dat has one image per region, with probability values
% convert to integer vector

[maxval, condf] = max(double(full(dat.probability_maps)),[], 2);   % double is safer

allempty = maxval == 0 | isnan(maxval);

condf(allempty) = 0;

dat.dat = int32(condf);
n_regions = num_regions(dat);
n_maps = find(any(full(dat.probability_maps) > 0,1));
if length(n_maps) < n_regions
    dropped_ind = find(~ismember(1:n_regions,n_maps));
    
    % note, labels must be last in this list or else it would end