Geographical Binning and evaluation of results
4 次查看(过去 30 天)
显示 更早的评论
Hello guys,
I drove though the city and measured. From this drive I extracted: time, lat, lot and my_var - the variable I was measuring.
1689×4 table
Var1 lat lot my_var
2021-04-12 10:50:02.000 51.18 14.431 20.68
2021-04-12 10:50:03.000 51.18 14.431 19.6
2021-04-12 10:50:05.000 51.18 14.431 19.7
2021-04-12 10:50:06.000 51.18 14.431 20.3
2021-04-12 10:50:07.000 51.18 14.431 20.21
We were performing those drives in lets say "normal traffic situation" so the speed we drove wary. This is the reason why I would like to create a geographical bins, 5x5m each seems to be right for my purposes.
I have used hista(lat,lon,0.0025) which gave me (I hope) right centers of the bins a counts how many measurements were taken in each.
%[latbin,lonbin,count] = hista(lat,lon,binarea)
[latbin, lonbin, count] = hista(lon,lat,0.0025);
results =[latbin, lonbin, count];
results =
14.4495 51.1668 2.0000
14.4500 51.1668 5.0000
14.4486 51.1673 2.0000
14.4491 51.1673 3.0000
14.4504 51.1673 4.0000
14.4467 51.1677 2.0000
14.4472 51.1677 2.0000
14.4477 51.1677 3.0000
14.4481 51.1677 2.0000
14.4504 51.1677 2.0000
Now, I would like to apply this on my measurements, but I got stuck. I think the right option for this should be DSEARCHN but I did notquite get it. I have to use it on lat and lon together.
Will appreciate any advice.
3 个评论
Adam Danz
2021-5-17
编辑:Adam Danz
2021-5-17
Also, according to the documentation,
"[latbin,lonbin,count] = hista(lat,lon,binarea) uses the bin size specified by the input binarea, which must be in square kilometers"
If you want 5x5 mile bins, wouldn't you need hista(lat,lon,12.9499) since 5 sq mi equals 12.9499 sq km?
Lastly, your comment "hista(lat,lon,0.0025)" correctly provides lat and lon in the first and second inputs but the code under your comment reverses those first two inputs, "[latbin, lonbin, count]=hista(lon,lat,0.0025);".
采纳的回答
Adam Danz
2021-5-18
编辑:Adam Danz
2021-5-18
Unfortunately hista()does not return a vector of bin numbers for each input coordinate which is hard to believe. According to the documentation, hista() outputs the bin centers so you just need to find which bin center each point is closest to. That's easily done in cartesian coordinates so I temporarily converted the (lon,lat) coordinate to equidistant (x,y) coordinates using grn2eqa (see Equal-Areas in Geographic Statistics) and pdist2 to compute distances between each coordinate and each bin center. The bin ID is then added to the table and the binned coordinates are plotted for verification.
Finally, the medians of each bin are computed.
This demo uses fake data and bins the coordinates at 2500 sq km.
% Produce some fake data
rng('default')
timestamp = datetime(2021,04,12,10,50,02) + seconds(cumsum(randi(5,1,100)))';
lat = rand(size(timestamp)) + 40;
lon = rand(size(timestamp))+10;
my_var = rand(size(timestamp))*5+15;
T = table(timestamp, lat, lon, my_var);
head(T) % view first few rows of the table
% bin at 2500 sq km
[latbin, lonbin, count] = hista(T.lat,T.lon,2500);
% Convert coordinates to equidistant cartesian coordinates
[T.latEq, T.lonEq] = grn2eqa(T.lat, T.lon);
[latbinEq, lonbinEq] = grn2eqa(latbin, lonbin);
% Compute distance between each coordinate and each bin-center
dist = pdist2([T.lonEq, T.latEq],[lonbinEq, latbinEq]);
% Add bin ID numbers to table
[~, T.bin] = min(dist,[],2);
head(T) % view updated table
% Verify
figure()
hold on
scatter(lonbin, latbin, 200, 1:numel(lonbin), 'Marker','*','LineWidth',2) % bin centers
scatter(T.lon, T.lat, 50, T.bin,'filled')
cmap = colorcube(255);
colormap(cmap(1:end-10,:))
xlabel('longitude'); ylabel('latitude')
latbinUnq = unique(latbin);
lonbinUnq = unique(lonbin);
set(gca, 'xtick', lonbinUnq(2:end)-diff(lonbinUnq)/2, ...
'ytick',latbinUnq(2:end)-diff(latbinUnq)/2)
grid on
Compute medians of each bin
binnedMedian = splitapply(@median,T.my_var, T.bin);
T2 = table((1:numel(lonbin))', lonbin, latbin, binnedMedian, ...
'VariableNames', {'binID','LonBin','LatBin','Med_my_var'})
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics and Visualization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!