How to expand lat/lon vectors into arrays for scatter plot

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

Note that you could instead use
mask = ncData == 1;
latData = lat(mask);
lonData = lon(mask);
scatter(lonData, latData, 1, 'green');
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 ?
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(:)
Chris LaPierre:
lat = reshape(lat,size(ncData)); % Error using reshape. Number of elements must not change.
latData = lat .* ncData(:) % Arrays have incompatible sizes for this operation.
Walter Roberson:
My original code for scatter plots works just fine for 2D lat and lon arrays (it may have some redundancies), but I can't get it to work for the case where lat and lon are 1D vectors. All of the suggestions so far result in errors. I haven't tried meshgrid or ndgrid yet.
mask = ncData == 1;
latData = lat(mask); % The logical indices contain a true value outside the array bounds.
lonData = lon(mask); % The logical indices contain a true value outside the array bounds.
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?
mask = ncData == 1;
[LatG, LonG] = ndgrid(lat, lon);
latData = LatG(mask);
lonData = LonG(mask);
These are satellite observations. When I plot the four min/max corners of the lat/lon vectors, I get a rectangle over a specific area of the Mercator map. So I think they are actual geo coordinates.
The observations are in the ncData table. The lat and lon vectors are supposed to define where the observations are, but I'm still at a loss as to how to interpret that, given that I only have two vectors to play with.
Use the ndgrid() solution that I posted.
Walter:
The ndgrid solution worked!
Thanks,
Kurt

Sign in to comment.

 Accepted Answer

Hey Kurt,
It looks like your 1D lat/lon vectors define a grid where the observations in ncData are located. To expand them into 2D arrays that match ncData, you need to use meshgrid or ndgrid.
Solution: Expand lat/lon Vectors into 2D Arrays
Since lat is 517×1 and lon is 318×1, they represent row and column coordinates. To match ncData (517×318), use:
[LatGrid, LonGrid] = ndgrid(lat, lon);
or equivalently:
[LonGrid, LatGrid] = meshgrid(lon, lat);
Now, LatGrid and LonGrid are 517×318 matrices, aligning with ncData.
Scatter Plotting with the Mask
Once you have 2D grids, apply the mask:
mask = ncData == 1; % Identify valid data points
latData = LatGrid(mask);
lonData = LonGrid(mask);
scatter(lonData, latData, 1, 'green'); % Scatter plot
This approach ensures that only valid data points are plotted, just like when your lat/lon were 2D arrays.
Let me know if you run into any issues! Follow me so you can message me anytime with future MATLAB questions. 🚀

More Answers (0)

Products

Release

R2023b

Asked:

on 7 Mar 2025

Commented:

on 7 Mar 2025

Community Treasure Hunt

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

Start Hunting!