필터 지우기
필터 지우기

Inpolygon returning a NAN

조회 수: 1 (최근 30일)
Simbarashe Chidzambwa
Simbarashe Chidzambwa 2022년 12월 15일
댓글: Simbarashe Chidzambwa 2022년 12월 15일
data=netcdf.open('gisst_full.nc','NC_NOWRITE')
lat=netcdf.getVar(data,0);
lon=netcdf.getVar(data,1);
time=netcdf.getVar(data,2);
sst=netcdf.getVar(data,3);
sst(sst==-32768)=NaN;
ti=datetime(1800,1,1)+days(time);
n=length(ti);
sst1=[sst(181:360,:,:);sst(1:180,:,:)];
lon1=[lon(181:360);lon(1:180)]
sst2=permute(sst1,[2,1,3]);
lonn=wrapTo360(lon1);
[X,Y]=meshgrid(lonn,lat);
idx=inpolygon(X,Y,[160 170],[-35 -25]);
data=sst2(:,:,1333:12:1584);
data1=mean(data,3),'omitnan';
Jan=mean(nonzeros(data1(idx))),'omitnan';
When I run the above script I get as far as data1 but the last line to calculate Jan value I am getting 'NaN' for Jan instead of the mean value of my inpolygon. The gisst data is monthly SST values and I just want the mean for an area 160-170E and 25-35S as specified in my idx. What am I missing?

채택된 답변

Steven Lord
Steven Lord 2022년 12월 15일
The inpolygon function can't return a NaN value. The problem is later on in your code. If you ask for the mean of an empty array the answer is NaN.
mean([])
ans = NaN
That implies that neither of the points in your inpolygon call are inside your polygon. You can check this by calling any. If it returns false, neither of the points are inside.
any([false false])
ans = logical
0
any([false true])
ans = logical
1
I can't offer any guidance for how to correct this problem since it's not clear to me from your code what your goal is. If you describe in words not code what the variables in your code represent (some of the names seem suggestive of their purposes, but suggestions can be deceiving) and describe what you intend this code to do (again in words not code) we may be able to offer some suggestions.
  댓글 수: 1
Simbarashe Chidzambwa
Simbarashe Chidzambwa 2022년 12월 15일
I am trying to extract a January average value from GISST data of SST from an area 160-170E and 25-35S represented by the line:
"idx=inpolygon(X,Y,[160 170],[-35 -25]);"
over a period of 21 years between 1982-2002 which is represented by the line:
"data=sst2(:,:,1333:12:1584);".
The scripts runs OK and even produces "data1" which contains the average of all the grid values but its only the extraction of the defined area in that last line which is not working.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by