MATLAB - 计算contour封闭等值线内面积 ( ContourMatrix + polyarea)

文章详细描述了在MATLAB中如何使用contour函数绘制等值线,并利用polyarea函数计算各封闭等值线内的面积,提供了两种方法:一是通过ContourMatrix,二是直接处理contour函数返回的C矩阵。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

计算contour封闭等值线内面积

步骤

1、画contour图,以得到句柄 [ C , h ]
2、得到每条等值线的坐标(C或者h.ContourMatrix
3、用polyarea计算面积

参考1 :用h

MATLAB Answers - A. Shokouhi

v=[2,3];
[x,y] = meshgrid(0:2,0:2);
z = [2 2 2;2 0 2;2 2 2]
[C, h] = contour(x, y, z, 2);
n = 0;
i = 1;
% ContourMatrix — 等高线定义 
%2%%%%%%%%%%% [ h.ContourMatrix == C ] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1行 等值线1_value     X1 X2 ... Xn   等值线2_value     X1 X2 ... Xm  ……
%2行 等值线1_点的数量n  Y1 Y2 ... Yn  等值线2_点的数量m  Y1 Y2 ... Ym  ……
sz = size(h.ContourMatrix,2);  
nn(1) = h.ContourMatrix(2,1); %等值线1_点的数量n
xx = h.ContourMatrix(1,2:nn(1)+1);%等值线1坐标XX1~Xn
yy = h.ContourMatrix(2,2:nn(1)+1);%等值线1坐标YY1~Yn
area(1) = polyarea(xx,yy);% 计算面积
while n+nn(i)+i < sz
    n = n + nn(i);
    i = i + 1;
    nn(i)=h.ContourMatrix(2,n+i);
    xx = h.ContourMatrix(1,n+i+1:n+nn(i)+i);
    yy = h.ContourMatrix(2,n+i+1:n+nn(i)+i);
    area(i) = polyarea(xx,yy);
end

参考2 :用C

参考MATLAB Answers - Vikas Sharma ,但有修改

[C,h] = contour(X,Y,Z,v);   % the coontour whose area is required
% contour lines at the data values specified in the monotonically increasing vector v.
clabel(C,h); colorbar; grid on
areas = zeros(1,length(v)); % The number of areas to be calculated
i = 1; k=1; % i is for area loop. k is another couter
if ~isempty(c)
    for i=1:length(v)  
        xx = c(1,k+1:k+c(2,k)); % x data of a particular contour line
        yy = c(2,k+1:k+c(2,k)); % y data of a particular contour line
        areas(i) = polyarea(xx,yy);
        k=k+c(2,k)+1; % Take our counter to the next target
        clear xx yy 
        if k>size(c,2)
            break
        end
    end
end
areas % to see the calculated areas
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值