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: bst_pca.m
    path of file: ./repos/brainstorm3/toolbox/math
    
    the code of the file until where you have to start completion: function OutputFiles = bst_pca(sProcess, sInputs, PcaOptions, AtlasList, isOutMatrix, OutTimeWindow, OutComment)
% BST_PCA: Dimension reduction with principal component analysis (PCA) for scouts, or unconstrained sources.
%
% USAGE:  OutputFiles = bst_pca(sProcess, sInputs, PcaOptions, AtlasList, isOutMatrix, OutTimeWindow)
%
% INPUTS:
%    - PcaOptions: Specifies PCA method and covariance settings, usually obtained in the calling
%      process through the PcaOptions panel, with defaults/preferences from bst_get('PcaOptions').
%      3 available PCA methods (PcaOptions.Method):
%          'pca'  : old approach (pre 2023-06), separately for each file, resulting in sign inconsistencies
%                   between files.
%          'pcaa' : *Across* files.  Computes a single "reference" component based on all the source data
%                   concatenated (per subject).
%          'pcai' : *Individual* files.  Computes a separate PCA component for each file, but correcting
%                   the sign so it aligns (positive projection) with the 'pcaa' reference component.
%    - AtlasList: If provided, run PCA to extract one time series per scout, per source orientation
%      (i.e. 3 per scout for unconstrained sources). If empty: flatten unconstrained sources
%      instead.  Both scout extraction and flattening cannot be run from the same bst_pca call. If
%      both are needed, it is recommended to flatten first, then do scout extraction.
%    - isOutMatrix: If true, force scout PCA to save 'matrix' type files; otherwise, saves 'results',
%      in kernel form when possible (when all inputs from a condition are kernel links).
%    - OutTimeWindow: Requested time window for output time series when isOutMatrix is true. It is
%      widened if needed to include baseline and data time windows that are used to compute the PCA
%      (in PcaOptions).
%    - OutComment: Optional, string to use for new node in tree, instead of default based on inputs.
%
% OUTPUTS:
%    - OutputFiles: This function can save and return deprecated atlas-based result files (full or
%    kernel), when provided with an AtlasList and isOutMatrix is false. These files can only be read
%    properly by process_extract_scout. By extension, they can be read by processes that use
%    bst_process('LoadInputFile', Target) with a scouts-type "Target" (e.g. bst_connectivity and
%    process_pac), or that call directly process_extract_scout (e.g. process_timefreq). These files
%    are deprecated but some Brainstorm
    
function can save and return deprecated atlas-based result files (full or
%    kernel), when provided with an AtlasList and isOutMatrix is false. These files can only be read
%    properly by process_extract_scout. By extension, they can be read by processes that use
%    bst_process('LoadInputFile', Target) with a scouts-type "Target" (e.g. bst_connectivity and
%    process_pac), or that call directly process_extract_scout (e.g. process_timefreq). These files
%    are deprecated but some Brainstorm processes create them through bst_pca temporarily, deleting
%    them after use (e.g. connectivity processes).
%
% IMPLEMENTATION NOTES:
%    PCA for scout extraction (when non empty AtlasList provided) with unconstrained sources could
%    in theory be done as a single step and provide a single time series; not requiring to flatten
%    separately first. However, to simplify the possible workflows, they must be done sequentially
%    in Brainstorm, flat then scout. (Note that for viewing scout timeseries through the GUI, this
%    function is bypassed and PCA is applied for both scouts and flattening together in
%    view_scouts.)
%
%    Scout PCA components (and so also the timeseries or kernel) are rescaled to have norm
%    1/sqrt(nVertices) instead of 1, to match the scale of the 'mean' component and be more
%    comparable to it, and to other scouts.  By definition, PCA will still always give timeseries
%    with more power than 'mean'.  This scaling is done in bst_scout_value.
%
%    The differences between the 3 PCA methods are all dealt with within this function (with one
%    exception pushed to bst_source_orient for convenience for now).  As such, 'pca2023' is used for
%    all 3 cases in other processes and functions.  'pca' in other functions was kept for the
%    previous (now deprecated) method with sign issues and without other improvements within this function.

% @=============================================================================
% This function is part of the Brainstorm software:
% https://neuroimage.usc.edu/brainstorm
% 
% Copyright (c) University of Southern California & McGill University
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
% 
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Marc Lalancette, 2023

OutputFiles = {};
AvgType = 2; % group by subject

if nargin < 7 || isempty(OutComment)
    OutComment = [];
end
if nargin < 6 || isempty(OutTimeWindow)
    OutTimeWindow = [];
end
if nargin < 5 || isempty(isOutMatrix)
    isOutMatrix = false;
end
if nargin < 4 || isempty(AtlasList)
    isScout = false;
else
    isScout = true;
end
% PcaOptions = sProcess.options.pcaedit.Value;
if nargin < 3 || isempty(PcaOptions) || isempty(sInputs)
    error('Missing inputs');
end

% Sort inputs to be able to efficiently re-use reference components.
[~, iSorted] = sort({sInputs.FileName});
sInputs = sInputs(iSorted);
% Group by subject.
iGroups = process_average('SortFiles', sInputs, AvgType); % = 2; by subject
if numel(iGroups) > 1
    % Run separately for each group, recursive call.
    for iG = 1:numel(iGroups)
        GroupOutputFiles = bst_pca(sProcess, sInputs(iGroups{iG}), PcaOptions, AtlasList, isOutMatrix, OutTimeWindow, OutComment);
        if isempty(GroupOutputFiles)
            OutputFiles = {};
            return; % Error already reported.
        end