How to expand lat/lon vectors into arrays for scatter plot
Show older comments
I am plotting a set of weather data over a Mercator projection. Most of the data arrives as 2D arrays containing lat and lon points, respectively:
lat 517x318 double array;
lon 517x318 double array;
ncData 517x318 double array containing ones and NaNs, used as a mask
I can scatter plot this data as follows:
latData = lat .* ncData;
lonData = lon .* ncData;
lonData(lonData == 0) = nan;
latData(latData == 0) = nan;
latLonData = scatter(lonData,latData,1,'green');
However, some of my data does not follow this pattern. In some cases, I get lat and lon files that are 1D vectors, not arrays. They appear to cover the range from max to min of the lat and lon coordinates in the ncData.
lat 517x1 vector
lon 318x1 vector
Is there a way to expand these two vectors into arrays, like the first instance, using interp2, say?
12 Comments
Walter Roberson
on 7 Mar 2025
Walter Roberson
on 7 Mar 2025
Note that you could instead use
mask = ncData == 1;
latData = lat(mask);
lonData = lon(mask);
scatter(lonData, latData, 1, 'green');
Walter Roberson
on 7 Mar 2025
lonData = lon .* ncData;
lonData(lonData == 0)
Your ncData is documented as being 1's and NaN. Multiplying lon by that gives lon in places and NaN in other places. Checking whether the result is equal to 0 is checking for original locations that are 0 where the mask is 1.
I have to wonder whether this is correct code. Are you truely wanting to zap places where lon was 0? Or are you being inconsistent on "ones and NaNs" and really the ncData is ones and zeros ?
Cris LaPierre
on 7 Mar 2025
Do the 1D vectors contain the same number of elements? If yes, how are they organized?
Since you are using scatter, there is no need to make your data 2D. You might be able to use reshape
% reshape
lat = reshape(lat,size(ncData));
or just the colon operator (but not both simultaneously).
% colon operatore
latData = lat .* ncData(:)
dpb
on 7 Mar 2025
The unanswered Q? is whether these are actually the lat, lon coordinates or the observations at an assumed lat, lon location based on other instances?
If the former, then where are the observations to go with the coordinates?
Walter Roberson
on 7 Mar 2025
mask = ncData == 1;
[LatG, LonG] = ndgrid(lat, lon);
latData = LatG(mask);
lonData = LonG(mask);
Walter Roberson
on 7 Mar 2025
Use the ndgrid() solution that I posted.
Kurt
on 7 Mar 2025
Accepted Answer
More Answers (0)
Categories
Find more on Resampling Techniques 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!