如何将两组数据按相同中心值分箱并取均值?
Got it, let's break this down and solve it for you! You want to bin both datasets A and B around the specified center points (0.2, 0.4, 0.6, 0.8), calculate the mean value for each bin (even when bin sizes are uneven), and visualize everything on the same plot. Here's a straightforward MATLAB solution:
Step 1: Define Your Binning Parameters
First, we'll set up the bin centers and calculate the corresponding bin edges. Since your centers are spaced 0.2 apart, we'll make each bin span 0.2 wide, so each center sits exactly in the middle of its bin:
% Define your target bin centers bin_centers = [0.2, 0.4, 0.6, 0.8]; % Set bin width to match the spacing between centers bin_width = 0.2; % Calculate bin edges (each bin is center ± bin_width/2) bin_edges = bin_centers - bin_width/2; bin_edges(end+1) = bin_centers(end) + bin_width/2; % Add the final right edge
Step 2: Create a Helper Function to Calculate Bin Means
This function will take a dataset and bin edges, then compute the mean Y-value for each bin. It handles empty bins gracefully by returning NaN (which MATLAB skips during plotting):
function bin_means = compute_bin_means(data, bin_edges) % Assign each X-value to a bin index bin_indices = discretize(data(:,1), bin_edges); % Initialize array to store bin means bin_means = zeros(length(bin_edges)-1, 1); for i = 1:length(bin_edges)-1 % Get indices of data points in the current bin in_bin = bin_indices == i; if any(in_bin) % Calculate mean Y-value for the bin bin_means(i) = mean(data(in_bin, 2)); else % Mark empty bins with NaN bin_means(i) = NaN; end end end
Step 3: Compute Binned Means for Both Datasets
Now apply the helper function to your datasets A and B:
% Your original data (from your example) A(:,1) = [0.05:0.05:0.80]'; A(:,2) = [ones(7,1); [0.6; 0.6; 0.4]; zeros(6,1)]; B(:,1) = [0.15:0.1:0.95]'; B(:,2) = [ones(4,1); [0.8; 0.8; 0.2]; zeros(2,1)]; % Calculate binned means A_bin_means = compute_bin_means(A, bin_edges); B_bin_means = compute_bin_means(B, bin_edges);
Step 4: Visualize the Results
Plot both the original data and the binned means together, so you can see how the grouped averages align with your target centers:
figure; % Plot original data points (optional, for reference) plot(A(:,1), A(:,2), 'o', 'Color', '#E24A33', 'DisplayName', 'Original A'); hold on; plot(B(:,1), B(:,2), 's', 'Color', '#348ABD', 'DisplayName', 'Original B'); % Plot binned means with lines connecting to centers plot(bin_centers, A_bin_means, '-ro', 'LineWidth', 1.5, 'MarkerSize', 8, 'DisplayName', 'Binned Mean A'); plot(bin_centers, B_bin_means, '-bs', 'LineWidth', 1.5, 'MarkerSize', 8, 'DisplayName', 'Binned Mean B'); % Add plot labels and formatting xlabel('X Value'); ylabel('Y Value'); title('Binned Data with Mean Values Around Specified Centers'); legend('Location', 'best'); grid on;
Key Notes
- The
discretizefunction handles assigning each X-value to the correct bin automatically, even if data points are unevenly distributed across bins. - Empty bins are marked with
NaN, so they won't show up in your plot (no weird zero values for bins with no data). - The bin width is set to match the spacing between your target centers, ensuring each center is the exact midpoint of its bin—this aligns perfectly with your requirement to bin around those specific values.
内容的提问来源于stack exchange,提问作者BenJHC

