Search code examples
matlabdirectiontrend

How to determine the direction of a slope in Matlab?


To determine the trend of earth, I need to determine the direction of slope using Matlab. My data is point cloud. In an area, I segment that area in windows and each window selects a ground point. For example, in an area some of the ground points are(X Y Z):

32512033.4400000    5401401.33000000    346.950000000000
32512044.0300000    5401399.54000000    346.850000000000
32512052.5800000    5401400.37000000    346.760000000000
32512065.0100000    5401401                 346.620000000000
32512073.5100000    5401399.99000000    346.480000000000
32512082.6900000    5401400.45000000    346.380000000000
32512094.1000000    5401401.41000000    346.330000000000
32512104.5300000    5401402.62000000    346.120000000000
32512114.5500000    5401401.42000000    345.860000000000
32512116.4800000    5401401.75000000    345.780000000000
32512033.7100000    5401409.82000000    347.120000000000
32512035.3900000    5401410.57000000    347.090000000000
32512052.7300000    5401415.40000000    350.110000000000
32512061.8500000    5401409.82000000    348.740000000000
32512065.8200000    5401415.45000000    359.700000000000
32512079.9800000    5401410.76000000    346.570000000000
32512093.4400000    5401409.58000000    349.620000000000
32512105.0100000    5401409.70000000    346.330000000000
32512114.9800000    5401409.66000000    346.170000000000

I plot the surface that passes from these points and figure here Now I want to determine the direction of slope, i.e. from down to up... or left to right or... How can I do this?


Solution

  • First question I would rise is: how did you fit your thing?

    It doesn't seem perfectly flat for a first order fit, but if you used

    sf=fit([X(:),Y(:)],Z(:),'poly11');
    

    then your answer is in sf, more particularly in sf.p01 (y direction) and sf.p10 (x direction).

    If you fitted with some secret function of yours, you can still use the function diff and average in X and Y direction.

    %Initialization with grid corresponding to your image
    [Xmat,Ymat]=ndgrid(-40:40,-60:60);
    Zmat=yourFunction(Xmat,Ymat);
    dZdX = diff(Zmat,1,1)./diff(Xmat,1,1);%X direction
    dZdY = diff(Zmat,1,2)./diff(Ymat,1,2);%Y direction
    SlopeX=mean(dZdX(:));
    SlopeY=mean(dZdY(:));
    

    You might need to think a little bit if the right answer doesn't come straight from it. In my case (with yourFunction = sf) it works perfectly.

    Cheers

    ps: if you're looking for the normal vector, hence giving you exactly the direction, you can compute it this way:

    %Normal vector to fitted surface
    n=-1.*[SlopeX, SlopeY, -1];
    n=-1.*[SlopeX, SlopeY, -1]./sqrt(n*n');