Obtaining mean values contourf

조회 수: 5 (최근 30일)
Carola Forlini
Carola Forlini 2023년 8월 21일
편집: DGM 2023년 8월 21일
Hi,
I am using the contourf function to plot data inside a specific level. After this, I would like to evaluate the mean value at each level (not the centroid) to be then used for further analysis.
So far I am able to exctract the data inside a specific level but then I am not able to find the mean values.
Can someone help?
Thank you,
Carola

채택된 답변

DGM
DGM 2023년 8월 21일
편집: DGM 2023년 8월 21일
Consider the following example
% some fake data
A = reshape(1:256,16,[]);
A = A + 10*randn(size(A));
[min(A(:)) max(A(:))] % just show what the data range is
ans = 1×2
-16.3551 266.1712
% unless levels are known explicitly
% get them from the contour plot
[~,hc] = contourf(A); hold on
% first level corresponds to data minimum
% last level rarely corresponds to data maximum
ll = hc.LevelList
ll = 1×7
-16.3551 0 50.0000 100.0000 150.0000 200.0000 250.0000
% so make sure there are consistently enough boundaries to cover the data range
mx = max(A(:));
if mx > ll(end)
ll = [ll mx];
end
ll % the full level list (the region boundaries)
ll = 1×8
-16.3551 0 50.0000 100.0000 150.0000 200.0000 250.0000 266.1712
% get the mean value of each region
nregions = numel(ll)-1;
lvlmean = zeros(1,nregions);
for k = 1:nregions
if k < nregions
% each region in a contourf() plot
% is associated with the lower boundary of the region
mask = (A >= ll(k)) & (A < ll(k+1));
else
% make sure to include maximum values
mask = (A >= ll(k)) & (A <= ll(k+1));
end
lvlmean(k) = mean(A(mask));
end
lvlmean
lvlmean = 1×7
-11.8971 25.6399 76.4761 127.1281 177.1200 222.5627 256.6190
... or you could choose to handle the region conditionals symmetrically if you wanted. That's up to you.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by