如何在Matlab中实现泊松分布的矩阵式计算?
Hey there! Let's break down how to fix those dimension mismatches and generate the exact 20×20 matrices you need for each pair of A/B values, then compute the region sums you're after. I'll walk through this step by step with code examples that match your Excel workflow.
First, Align with Your Excel Logic
In Excel, you’re calculating POISSON(x, μ, FALSE) where x starts at 0 (like cell AD313 uses x=0 for both A and B). So in MATLAB, we need to use z = 0:19 (20 values total, from 0 to 19) instead of 1:20—that’s one key fix for your earlier dimension error.
Step 1: Import Your Data
First, load your CSV data properly (assuming A is column 1, B is column 2):
% Load the CSV file data = readmatrix('your_data.csv'); A = data(:, 1); % 110×1 vector of A values B = data(:, 2); % 110×1 vector of B values
Step 2: Generate the Grid for Matrix Indices
We need a 20×20 grid where each row corresponds to x = row-1 (for A) and each column corresponds to y = column-1 (for B). Use meshgrid to create this:
z_values = 0:19; % Matches your Excel x=0 to x=19 (20 values total) [Z_row, Z_col] = meshgrid(z_values, z_values); % Z_row: 20×20 matrix where each row is [0,1,...,19] % Z_col: 20×20 matrix where each column is [0,1,...,19]
Step 3: Batch Generate All 110 Matrices
To avoid slow loops, we’ll use MATLAB’s broadcasting feature by reshaping A and B into 3D arrays. This lets us compute all 110 matrices at once:
% Reshape A and B to 1×1×110 to enable broadcasting with the 20×20 grids A_3d = reshape(A, 1, 1, []); B_3d = reshape(B, 1, 1, []); % Calculate the Poisson product for every cell in every matrix poisson_matrices = poisspdf(Z_row, A_3d) .* poisspdf(Z_col, B_3d); % Result: 20×20×110 array, where poisspdf(:,:,i) is the matrix for A(i) and B(i)
Step 4: Sum Specific Regions of Each Matrix
Now compute the sum for your target region. Let’s use an example—adjust the mask to match your actual region:
% Define your region mask (customize this to your exact needs!) % Example: Sum all cells where x + y ≤ 5 (x = row-1, y = column-1) region_mask = (Z_row + Z_col) <= 5; % Sum the masked region for each matrix region_sums = squeeze(sum(sum(poisson_matrices .* region_mask, 1), 2)); % squeeze converts the 1×1×110 result to a clean 110×1 vector
If your target region is based on row/column ranges (e.g., rows 1-10 and columns 5-15), adjust the mask like this:
% Rows 1-10 → x = 0-9; Columns 5-15 → y = 4-14 region_mask = (Z_row >= 0 & Z_row <=9) & (Z_col >=4 & Z_col <=14);
Step 5: Add Sum Results as a New Column
Finally, append the sums to your original data and save it back to CSV:
% Combine original data with the new sum column data_with_sums = [data, region_sums]; % Save to a new CSV file writematrix(data_with_sums, 'your_results_with_sums.csv');
Why Your Earlier Code Had Issues
- You used
z=1:20instead of0:19, which didn’t align with Excel’s starting x=0. - When you tried
Poisspdf(z,A), MATLAB couldn’t broadcast a 1×20 vector with a 110×1 vector. Reshaping A/B to 3D arrays lets MATLAB automatically apply each A/B pair to the entire 20×20 grid.
内容的提问来源于stack exchange,提问作者blowbuh

