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

3 Ansichten (letzte 30 Tage)
Kurt
Kurt am 7 Mär. 2025
Kommentiert: Kurt am 7 Mär. 2025
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 Kommentare
Walter Roberson
Walter Roberson am 7 Mär. 2025
Use the ndgrid() solution that I posted.
Kurt
Kurt am 7 Mär. 2025
Walter:
The ndgrid solution worked!
Thanks,
Kurt

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jack
Jack am 7 Mär. 2025
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. 🚀

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by