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: CBIG_regress_X_from_y_train.m
    path of file: ./repos/CBIG/utilities/matlab/predictive_models/utilities
    
    the code of the file until where you have to start completion: function [y_hat, beta] = CBIG_regress_X_from_y_train(y, X)

% [y_hat, beta] = CBIG_regress_X_from_y_train(y, X)
% 
% This function regress covariates X (e.g. head motion) from y (e.g.
% behavioral scores) in the training set (for any preditive model).
% 
% Inputs:
%   - y 
%     A #TrainingSubjects x 1 vector.  Each row in the matrix contains the
%     target variable value of each subject.
% 
%   - X
%     A #TrainingSubjects x #covariates matrix of
    
function only regresses a vector
%     of ones from y (i.e. demean).
% 
% Outputs:
%   - y_hat
%     A #TrainingSubjects x 1 vector of residual target variable values
%     after regression.
% 
%   - beta
%     A #covariates x 1 vector of regression coefficients used to regress
%     the training set target variables.
%
% Written by CBIG under MIT license: https://github.com/ThomasYeoLab/CBIG/blob/master/LICENSE.md
% Authors: Ru(by) Kong, Jingwei Li


% check y in case there is nan
nan_index_y = isnan(sum(y,2));
% check X in case there is nan, exclude this subject for all covariates
nan_index_X = isnan(sum(X,2));

if(~isempty(X))
    nan_index = (nan_index_y | nan_index_X);
else
    nan_index = nan_index_y;
end