Further processing of contour plot data

8 次查看(过去 30 天)
Hello,
I would like to analyse a contour plot data.I’m trying to divide the contour plot into 4x3 pieces, calculate the mean for every data “rectangle” (4x3 rectangles=12 mean values).
I gather the contour data with this code below:
clc,close all
x = linspace(-pi,pi);
y = linspace(0,2*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X) + cos(Y);
[C,h] = contourf(X,Y,Z,10)
I'm struggeling to code the divison of the contour data into smaller areas, with subsequent mean calculation.
Maybe someone has a clue to achieve this
Greetings
Steffen

采纳的回答

Mathieu NOE
Mathieu NOE 2023-1-12
hello
maybe this ?
clc,close all
clearvars
N = 100; % <= added
x = linspace(-pi,pi,N);
y = linspace(0,2*pi,N);
[X,Y] = meshgrid(x,y);
Z = sin(X) + cos(Y);
figure(1)
[C,h] = contourf(X,Y,Z,10);
hold on
% let(s do a 4 x 3 area division and compute the individual cells average
Nx = 4;
Ny = 3;
dx = floor(N/Nx);
dy = floor(N/Ny);
for ci = 1:Nx
for cj = 1:Ny
indX = (1:dx)+(ci-1)*dx;
indY = (1:dy)+(cj-1)*dy;
% centroid of cell
xc = mean(x(indX));
yc = mean(y(indY));
% mean of submatrix
Zmean(ci,cj) = mean(Z(indX,indY),'all');
plot(xc,yc,'*r','Markersize',15);
t = text(xc+0.15,yc+0.15,num2str(Zmean(ci,cj),3),'FontSize',14,'Color','r');
end
end
  2 个评论
Steffen B.
Steffen B. 2023-1-16
Hello Mathieu,
thanks a lot for your help.
Your script is working just fine.
Greetings
Steffen

请先登录,再进行评论。

更多回答(1 个)

Bjorn Gustavsson
Bjorn Gustavsson 2023-1-12
I would not do the average of the contours in a region, because if you have a rather flat region with a single spike in some region you will have very few contours at the flat level, but a large number of contours and contour-points around the spike. If you then take the average of the z-levels of the contour-points it will be significantly skewed away from the flat level. I would make some surface-fit from the conours, either with the spline-fitting functions spap2, spapi, spaps, csaps or tpaps, or use scatteredInterpolant or griddata - because that way I would obtain a sampling that is regular in the x and y-directions.
Regardless of approach you'll need something that extracts the x y and z coordinates from the contours. For this you'll need something like the function extract_contours (attached).
Using this I'd do something like this:
% Mocking up some data:
x = 1:123;
y = 1:123;
z = peaks(123);
nC = 10;
C = contour(x,y,z,nC); % this give us a contour-data-set
[xC,yC,zC] = extract_contours(C); % extracting contour-levels
F = scatteredInterpolant(cell2mat(xC)',cell2mat(yC)',cell2mat(zC)','natural');
xi = 1:2:123;
[xi,yi] = meshgrid(xi);
zi = F(xi,yi); % calculating the interpolated values at [xi,yi]
xlims = [0, 40.5 92.3 124];
ylims = [0 82.3 124];
for i1 = (numel(ylims)-1):-1:1
for i2 = (numel(xlims)-1):-1:1
zAVG(i1,i2) = mean(zi(ylims(i1)<yi(:,1)&yi(:,1)<ylims(i1+1),xlims(i1)<xi(1,:)&xi(1,:)<xlims(i2+1)),'all','omitnan');
end
end
If you want to average over the contours you could proceed something like this after the contour-extraction:
xAll = cell2mat(xC)';
yAll = cell2mat(yC)';
zAll = cell2mat(zC)';
xlims = [0, 40.5 92.3 124];
ylims = [0 82.3 124];
for i1 = (numel(ylims)-1):-1:1
for i2 = (numel(xlims)-1):-1:1
zAVG(i1,i2) = mean(zAll(ylims(i1)<yAll & ...
yAll<ylims(i1+1) & ...
xlims(i1)<xAll & ...
xAll < xlims(i2+1)),'all','omitnan');
end
end
I think that should work.
HTH
  2 个评论
Steffen B.
Steffen B. 2023-1-16
编辑:Steffen B. 2023-1-16
Hello Björn,
thanks a lot for your help.
Your script is working just fine.
Unfortunately it is not possible to accept two answers at the same time.
Mathieu was faster, therefore he got the point.
Sorry for this, your answer deserved it too.
Greetings
Steffen
Bjorn Gustavsson
Bjorn Gustavsson 2023-1-16
Well, our answers addressed two completely different problems. I interpreted your problem as if you had only the contours and wanted the averages in different regions estimated from that, and that was obviously not the problem you had. But if you happen to run into that type of problem you already have one solution here.
Cheers.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by