Minimum value within contour

25 views (last 30 days)
Konvictus177
Konvictus177 on 4 Feb 2022
Answered: Voss on 4 Feb 2022
Hi,
I would like to find the minimum value within a contour.
Let's say I have the following code that draws contours around all values that are lower than a set threshold. How can I find the minimum value within or for each contour and save it to a variable, list or table?
[X,Y,Z] = peaks(100);
figure(1),hold on
s = surf(X,Y,Z,'Facealpha',0.75,'Edgecolor','none');
view(2);
colorbar('vert');
threshold = -0.3*max(Z,[],'all');
[c,h] = contour(X,Y,Z,[threshold threshold],'-k','Linewidth',5);
hold off

Accepted Answer

Voss
Voss on 4 Feb 2022
You can use the information in c along with inpolygon() and logical indexing in Z to do it:
[X,Y,Z] = peaks(100);
figure(1),hold on
s = surf(X,Y,Z,'Facealpha',0.75,'Edgecolor','none');
view(2);
colorbar('vert');
threshold = -0.3*max(Z,[],'all');
[c,h] = contour(X,Y,Z,[threshold threshold],'-k','Linewidth',5);
hold off
Z_min = [];
idx = 1;
N = size(c,2);
while idx < N
pts = c(:,idx+(1:c(2,idx)));
in_region = inpolygon(X,Y,pts(1,:),pts(2,:));
Z_min(end+1) = min(Z(in_region));
text(mean(pts(1,:)),min(pts(2,:)),max(Z(:)), ...
sprintf('Min Z: %f',Z_min(end)), ...
'VerticalAlignment','top', ...
'BackgroundColor','w');
idx = idx+c(2,idx)+1;
end
disp(Z_min);
-6.5419 -3.0474

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!