How to extract values within a circular region from a griddata?

조회 수: 19 (최근 30일)
aneps
aneps 2022년 6월 23일
댓글: Bjorn Gustavsson 2022년 6월 23일
I have a 2d matrix (100 x 100 size) obtained from the piece of code is as follows:
N = 100;
xv = linspace(min(CoordsX), max(CoordsX), N);
yv = linspace(min(CoordsY), max(CoordsY), N);
[X,Y] = ndgrid(xv, yv);
Data = griddata(CoordsX, CoordsY, Field, X, Y);
figure()
surf(X, Y, Data) % This make a surface plot of Data.
'Data' outputs a 100 x 100 matrix and then I plot a surface plot as it is in the code. Here I want to select a region with in a circle centered at xc, yc and radius 'r'. Then make another surface plot. How can I extract the vlaues from 'Data' those are within a circular region or radius 'r'?
Thank you

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2022년 6월 23일
You "simply" only ask for the values inside that circular region. You can do it at least two different ways:
N = 100;
xv = linspace(min(CoordsX), max(CoordsX), N);
yv = linspace(min(CoordsY), max(CoordsY), N);
[X,Y] = ndgrid(xv, yv);
Data = griddata(CoordsX, CoordsY, Field, X, Y);
figure()
subplot(2,2,1)
surf(X, Y, Data) % This make a surface plot of Data.
Ioutside = find((X(:)-xc).^2+(Y(:)-yc).^2>r);
Data(Ioutside) = nan;
surf(X, Y, Data) % This make a surface plot of Data inside the circular region.
phi360 = (0:360)*pi/180;
r = linspace(0,r);
[Phi360,R] = meshgrid(phi360,r);
Data = griddata(CoordsX, CoordsY, Field, R.*cos(Phi360), R.*sin(Phi360));
subplot(2,2,3)
surf(R.*cos(Phi360), R.*sin(Phi360), Data) % This make the surface-plot with cylindrical coords
You could(should) also take a look at the scatteredInterpolant function since that might be a useful and more flexible multi-call replacement of the griddata-function.
HTH
  댓글 수: 2
aneps
aneps 2022년 6월 23일
super.. that worked.. thank you.
Bjorn Gustavsson
Bjorn Gustavsson 2022년 6월 23일
Good, happy that it helped - and my pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by