Correlation between spatially averaged gridbox and global gridpoints

16 views (last 30 days)
An issue I've been having in Matlab as I learn to use it more efficiently has brought up a question about the way things are done.
In the IRIDataLibrary, I can select global Precip data (all gridpoints) and SST data (averaged for Niño 3 region gridboxes), and map their correlation. However, when I export this data to Matlab, the dimensions of each dataset are mismatched, so I cannot perform a manual correlation test. (The reason I want to do the correlation in Matlab is that there seems to be an error in the way Matlab reproduces the IRIDL's correlation contour map for certain seasons.)
What am I doing wrong here? How does the IRIDL reconcile the dimension mismatch but Matlab does not?
My code:
prcp=ncread('http://iridl.ldeo.columbia.edu/SOURCES/.NOAA/.NCEP/.CPC/.Merged_Analysis/.monthly/.latest/.ver2/.prcp_est/T/%281986-2015%29/VALUES/T/monthlyAverage/T/%28Oct-Dec%29/seasonalAverage/dods', 'prcp_est');
SST=ncread('http://iridl.ldeo.columbia.edu/SOURCES/.NOAA/.NCDC/.ERSST/.version4/.sst/X/-150/-90/RANGEEDGES/Y/-5/5/RANGEEDGES/T/%281986-2015%29/RANGE/T/monthlyAverage/T/%28Oct-Dec%29/seasonalAverage/%5BX/Y%5Daverage/dods', 'sst');
nx=144; ny=72; nt=30; corp=zeros(nx,ny);
for x=1:nx
for y=1:ny
if prcp(x,y,1) ~= nan
[r,p]=corrcoef(prcp(x,y,:),SST);
corp(x,y)=r(1,2);
else
corp(x,y)=nan;
end
end
end
cn = 20; c1 = [255, 0, 0]; c2 = [255, 255, 255]; c3 = [255, 255, 255]; c4 = [0, 0, 255]; cmap=interp1([1,cn/2,cn/2+1,cn],[c4;c3;c2;c1]/255,1:cn);
lat=-5:0.05:5; lon=-150:0.05;-90; [LON LAT]=meshgrid(lon,lat);
figp=corp';
figure(1)
contourf(lon,lat,figp,-1:0.1:1,'linestyle','none')
title('precipitation and time series correlation') hold on
axis([-150 -90 -25 50])
colormap(cmap) colorbar('location','eastoutside')
caxis([-1 1])
Much of this is irrelevant because I receive an 'Line 61' error on 'contourf' function saying "X must match size of Z".
I have done this before just fine with matrices of identical dimensions. Any insight would be wonderful.

Accepted Answer

Cam Salzberger
Cam Salzberger on 25 Jul 2017
Hey Cory,
Your z-data, as you know, is a 72x144 matrix. It's been processed, but it's still the same size (if transposed) as when it was read from the data file. The x and y values you are trying to plot it at are 201x151. This is clearly going to cause a dimension mismatch. I am specifically using "x" and "y" values here, because contourf is a simple grid-based plot, and not specific to geographic coordinates.
The reason that this has to match exactly is that MATLAB is a general purpose programming language, not something specific to mapping, and you are using some general purpose plotting functions. So if you tell MATLAB to plot 151 points in a row, but only give it 144 heights to use, it will not work.
Now what it sounds like you want is either: 1) Provide the latitude and longitude of those data points you read from the data file exactly, and do the contour.
or
2) Interpolate the data at the lat/lon you specified, and do the contour with that.
For (1), you can just determine the endpoints of the range of the data, and then remake the LAT and LON variables. I'd suggest linspace over colon-operator just to avoid confusion about number of points.
For (2), you can use interp2, griddedInterpolant, or even geointerp if you have Mapping Toolbox and R2017a+.
Some other advice:
  • If you have the Mapping Toolbox, I'd suggest using those specific functions (like geointerp or contourfm), since they often help to put things in more familiar contexts.
  • Don't use == or ~= to compare to NaN, it will not do what you want. IEEE standard allows or maybe even mandates that NaN == NaN returns false. What you want is to use isnan.
  • I'd suggest against hardcoding your loop limits here. It makes much more sense to get nx, ny, and nt off of a call to size, rather than hardcoding them.
Hope this helps!
-Cam

More Answers (0)

Categories

Find more on Data Distribution 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!