MATLAB Answers

Selecting parts of gridded data- I Need Help. I want to know it's possible or not?

2 views (last 30 days)
Behzad Navidi
Behzad Navidi on 16 Feb 2020
Commented: Behzad Navidi on 21 Mar 2020
Hey all hope you have a good time,
I have a (1 x 1171) cell which includes tables. Each table has a specific latitude and longitude (columns). All 1171 tables latetudes and longitudes together represent gridded precipitation data over the whole country in the 0.5 x 0.5 degree (longitude x latitude).
I also have some points data (in the form of a table with columns of longitude, latitude, and precipitation).
I want to delete each grid in my cell that doesn't have at least one of my points inside it.
For example: If red dots are my points (in the table that I mentioned above), I want to keep each grid that has at least one red dot inside it (I would like to keep grid if more than one point is in it). So from 130 grids in this figure below I want to keep only nine grids that have points. So if my cell was 130x336 now it is just 9x336.
I attach my cell and my points.
Thank you so much for your valuable advice.

  3 Comments

darova
darova on 16 Feb 2020
It would be great if you will make a simple drawing of the result you want to see
Behzad Navidi
Behzad Navidi on 16 Feb 2020
Dear darova,
First of all, I want to appreciate you for reading my question. I upload grid again in this comment with the same format as points.
It would be great if you will make a simple drawing of the result you want to see
I have gridded_datamat.mat when I plot them I see a 0.5 x 0.5 degree (lon x lat) pixels (grids) on map. On the other hand, I have points.mat (scatter) that when I add them on this grids, some of the grids has owned one, two, or more point inside and some grids haven't any point inside itself. I want to delete these grids that don't match with any of the points from gridded_datamat.mat.
The output is something like the gridded_datamat.mat that I uploaded here but with lower row than 1171 because undoubtedly many of grids haven't a chance to own at least one the points (I want to save each grid that at least has one point or higher).
I truly appreciate you. Please don't hesitate to contact me if something I've written isn't clear.
Best Regards

Sign in to comment.

Accepted Answer

darova
darova on 17 Feb 2020
Here is my achievement
clc,clear
load points.mat
load grid_datamat.mat
p = table2array(Points);
g = table2array(grid_data(:,2:3));
x = floor( (p(:,2)-24.25)/0.5 )*0.5+24.25;
y = floor( (p(:,3)-44.25)/0.5 )*0.5+44.25;
plot(p(:,2),p(:,3),'.r')
hold on
plot([x x+0.5 x+0.5 x x]',[y y y+0.5 y+0.5 y]','.-b','markersize',15)
plot(g(:,2),g(:,1),'.g')
hold off

  7 Comments

Show 4 older comments
Behzad Navidi
Behzad Navidi on 18 Mar 2020
Dear darova,
Hope you are well.
I have a little problem here, I would be grateful if you can help me. The accepted answer code works perfectly with my attached data to this question.
I wanted to use this code for another data sets with different latitude and longitude values that I attached now in this comment, But I don't know why some issues happen during doing that.
May I ask you please look at my new attached data and guide me on how to modify the code? Because I have some problem as I listed below:
1. As this figure below shows, my first comment problem occurred here again (four green points Being involved even when I delete 0.25. I want to select only one green point that has redpoint(s) (based on 0.5 x 0.5 degree that I mentioned that exist for each green point) (All green points are one centroid (center of a pixel, when the pixel is in 0.5 x 0.5-degree resolution (square))) I want something like grey boxes.
2. To solve this problem I try to add 0.25 in these lines:
x = floor( (p(:,3)-24.25)/0.5 )*0.5+24.25;
y = floor( (p(:,2)-44.25)/0.5 )*0.5+44.25;
at the first look, I thought it, okay but then I saw in some area, the wrong box draw over red points for example here:
when I measurement right and left, and up and down distance of green point by ruler I saw that they are not equal (0.5 * 0.5)
Now I don't know why this code does not work well for my new data.
Do you know how should I do?
Thank you so much again
darova
darova on 18 Mar 2020
I believe it happens because of new mesh (0.025)
>> g(1,:)
ans =
44.0250 24.0250
Little improvement
x0 = g(1,2) + 0.25;
y0 = g(1,1) + 0.25;
x = floor( (p(:,2)-x0)/0.5 )*0.5+x0;
y = floor( (p(:,3)-y0)/0.5 )*0.5+y0;
looks equal

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!