forked from amforte/Topographic-Analysis-Kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindCentroid.m
46 lines (36 loc) · 821 Bytes
/
FindCentroid.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function [Cx,Cy]=FindCentroid(DEM)
%
% Usage:
% [Cx,Cy]=FindCentroid(DEM);
%
% Description:
% Function to find centroid of drainage basin
%
% Required Input:
% DEM - GRIDobj
%
% Output:
% [Cx,Cy] - two 1x1 matrices, Cx and Cy with x and y positions of centroid in same coordinates as DEM
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function Written by Adam M. Forte - Updated : 06/18/18 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I=isnan(DEM);
I=~I;
Imat=GRIDobj2mat(I);
Imat=double(Imat);
[X,Y]=getcoordinates(DEM);
Ix=bsxfun(@times,Imat,X);
Iy=bsxfun(@times,Imat,Y);
Ixs=nonzeros(Ix);
Iys=nonzeros(Iy);
Cxi=mean(Ixs);
Cyi=mean(Iys);
x_dist=abs(X-Cxi);
y_dist=abs(Y-Cyi);
[~,Ixx]=min(x_dist);
[~,Iyy]=min(y_dist);
Cx=X(Ixx);
Cy=Y(Iyy);
%Function End
end