function Data=RemoveModelOutliers(Data)
%Remove ROIs that are outliers in term of feature values
temp=Data.FeatureVal;
index=(temp-repmat(mean(temp,1,'omitnan'),[size(temp,1),1,1]))./(repmat(std(temp,1,'omitnan'),[size(temp,1),1,1]))>3.28;
index=(sum(index,2,'omitnan')>0);
Data.FeatureVal(repmat(index,[1,size(temp,2),1]))=NaN;
Data.PosVal(repmat(index,[1,3,1]))=NaN;

%Now remove distance outliers. Outliers are those pairs of points for
%which the distance between the center of mass of two ROI is an outlier.
%The threshold for outlier detection is set at 0.1% based on the assumption
%of normal distribution. If an outlier is detected, all three space
%directions are set to NaN before data imputation.
temp=Data.PosVal;
RemoveIndex=false(size(temp));
for k=1:3
  for i=1:size(temp,3)
    for j=i+1:size(temp,3)
      PosDiff=temp(:,k,i)-temp(:,k,j);
      MeanDiff=mean(PosDiff,'omitnan');
      StdDiff=std(PosDiff,1,'omitnan');
      index=(abs(PosDiff-MeanDiff)/StdDiff)>3.28;
      RemoveIndex(index,:,i)=true;
      RemoveIndex(index,:,j)=true;
    end
  end
end
Data.PosVal(RemoveIndex)=NaN;
RemoveIndex=repmat(RemoveIndex(:,1,:),[1,size(Data.FeatureVal,2),1]);
Data.FeatureVal(RemoveIndex)=NaN;
end