function b = trend_sen(data)
% Calculates Theil-Sen slope estimator for multiple time series
%
% Input:
%   data: 2D array (n_series × time) where each row is a separate time series
%
% Output:
%   b: Vector of Theil-Sen slope estimates for each time series
%
% Method:
%   Theil-Sen estimator is a robust linear regression method that calculates
%   the median of slopes between all possible pairs of points in the time series
%
% Dependencies:
%   - Theil_Sen_Regress: Function to compute Theil-Sen slope for single time series

    % Initialize output array with NaNs
    b = nan*zeros(size(data,1),1);
    
    % Loop through each time series
    for i = 1:size(data,1)
        % Skip if time series starts with NaN (invalid data)
        if isnan(data(i,1)); continue; end
        
        % Calculate Theil-Sen slope using time indices as x-coordinates
        % [1:size(data,2)] creates time index vector
        % data(i,:) extracts the i-th time series
        b(i) = Theil_Sen_Regress([1:size(data,2)]', data(i,:)');
    end
    
end