You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matlab中多区域边界绘制:相图区域分割线自动生成

Got it, let's work through this phase boundary plotting problem together! I'll show you exactly how to extract and draw those two key boundary lines from your phases array in MATLAB, just like you need.

Step 1: Break Down the Problem

First, let's clarify your input: your phases array is a 2D grid where each element represents the phase at that (x,y) coordinate (columns = x-axis, rows = y-axis). We need to find two types of boundaries:

  • Between phase 1 and phase 2
  • Between the combined phases 1/2 and phase 3
Step 2: Extract Boundary Coordinates

To find these boundaries, we'll check every pair of adjacent grid cells (horizontal and vertical neighbors). When two adjacent cells belong to the phase groups we care about, we'll record the midpoint between them as a boundary point.

Here's the code to do that:

% Your example phase array
phases = [1 1 1 1 2 2 2 2;
          1 1 1 2 2 2 2 2;
          1 1 2 2 2 2 2 2;
          3 3 3 3 3 3 3 3;
          3 3 3 3 3 3 3 3];

% Get grid dimensions (ny = number of rows/y-values, nx = columns/x-values)
[ny, nx] = size(phases);

% Initialize empty arrays to store boundary points
boundary_1_2 = [];       % Between phase 1 and 2
boundary_12_3 = [];      % Between phases 1/2 and 3

% Check horizontal adjacent cells (same row, side-by-side columns)
for y = 1:ny
    for x = 1:nx-1
        current_phase = phases(y, x);
        next_phase = phases(y, x+1);
        
        % Check for 1<->2 boundary
        if (current_phase == 1 && next_phase == 2) || (current_phase == 2 && next_phase == 1)
            boundary_1_2 = [boundary_1_2; x+0.5, y];  % Midpoint x = column + 0.5
        end
        
        % Check for (1/2)<->3 boundary
        if ((current_phase == 1 || current_phase == 2) && next_phase == 3) || ...
           (current_phase == 3 && (next_phase == 1 || next_phase == 2))
            boundary_12_3 = [boundary_12_3; x+0.5, y];
        end
    end
end

% Check vertical adjacent cells (same column, stacked rows)
for x = 1:nx
    for y = 1:ny-1
        current_phase = phases(y, x);
        next_phase = phases(y+1, x);
        
        % Check for 1<->2 boundary
        if (current_phase == 1 && next_phase == 2) || (current_phase == 2 && next_phase == 1)
            boundary_1_2 = [boundary_1_2; x, y+0.5];  % Midpoint y = row + 0.5
        end
        
        % Check for (1/2)<->3 boundary
        if ((current_phase == 1 || current_phase == 2) && next_phase == 3) || ...
           (current_phase == 3 && (next_phase == 1 || next_phase == 2))
            boundary_12_3 = [boundary_12_3; x, y+0.5];
        end
    end
end
Step 3: Plot the Boundaries

Now that we have all boundary points, we can plot them. We'll also add a heatmap of the phases as a reference (you can remove this if you don't need it):

figure;
% Optional: Plot phase regions as a heatmap
pcolor(phases);
shading flat;
colormap(viridis);  % Use a nice color map (adjust as needed)
hold on;

% Sort boundary points to connect them smoothly (optional but recommended)
boundary_1_2 = sortrows(boundary_1_2, [2 1]);  % Sort by y, then x
boundary_12_3 = sortrows(boundary_12_3, [2 1]);

% Draw the two boundary lines
plot(boundary_1_2(:,1), boundary_1_2(:,2), 'k-', 'LineWidth', 2);  % Black line for 1-2
plot(boundary_12_3(:,1), boundary_12_3(:,2), 'r-', 'LineWidth', 2);  % Red line for 1/2-3

% Adjust axes to match the grid structure
axis([0.5 nx+0.5 0.5 ny+0.5]);
set(gca, 'YDir', 'reverse');  % Makes y-axis start at the top (matches matrix indexing)

% Add labels and legend
xlabel('X Coordinate');
ylabel('Y Coordinate');
title('Phase Boundaries');
legend('Boundary: Phase 1 ↔ Phase 2', 'Boundary: Phases 1/2 ↔ Phase 3');
hold off;
Step 4: Customize as Needed
  • If you don't want the phase heatmap, just remove the pcolor and shading lines.
  • Adjust line colors, widths, or styles by modifying the plot arguments (e.g., 'b--' for blue dashed lines).
  • If your phase array is larger or has more complex boundaries, this code will still work—it automatically checks all adjacent cell pairs.

This will give you exactly the two clean boundary lines you're looking for, matching the example plot you mentioned!

内容的提问来源于stack exchange,提问作者Mathias Kure

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:21:50