function X = bin_data_to_X(PAR, dO2anom, PARbin)
    % Check if PAR and dO2anom have the same length
    if length(PAR) ~= length(dO2anom)
        error('PAR and dO2anom must have the same length.');
    end
    
    % Initialize X matrix to store binned data
    num_bins = length(PARbin)-1;
    X = zeros(num_bins, 1);
    
    % Loop through each bin
    for i = 1:num_bins
        % Find indices of data points within the current bin
        bin_indices = find(PAR >= PARbin(i) & PAR <= PARbin(i+1));
        
        % Calculate the average value of dO2anom within the bin
        if ~isempty(bin_indices)
            X(i) = nanmean(dO2anom(bin_indices));
        else
            X(i) = NaN; % No data points in this bin
        end
    end
end
