%--------------------------------------------------------------------------
%Copyright 2016, Mengqiu Li

%The following code is licensed under a Creative Commons 
%Attribution-NonCommercial-ShareAlike 4.0 International Public License.

%Under the following terms:

%1) Attribution - You must give appropriate credit, provide a link to the 
%license, and indicate if changes were made. You may do so in any 
%reasonable manner, but not in any way that suggests the licensor 
%endorses you or your use. 

%Preferred method of attribution is citing or referring to the published 
%paper associated with this work: 
%Journal of the American Chemical Society, 2015, 137 (51), pp 16055–16063.

%2) NonCommercial - You may not use the material for commercial purposes.

%3) ShareAlike - If you remix, transform, or build upon the material, 
%you must distribute your contributions under the same license as the 
%original.

%4) No additional restrictions - You may not apply legal terms or 
%technological measures that legally restrict others from doing anything 
%the license permits.

%Under the above terms, you are free to:

%1) Share - copy and redistribute the material in any medium or format
%2) Adapt - remix, transform, and build upon the material

%The license in full: 
%   http://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
%--------------------------------------------------------------------------

function LineCoefficient=turningpointcheck(TurningPoints,X)
    %Mark the sections of a line according to the predetermined
    %turning points(or section divider), e.g.:
    %line:[1,2,3,4,5,6,7,8,9,10]; turning at 3 and 7.
    %output(:,:,1):  1,1,1,0,0,0,0,0,0,0
    %output(:,:,2):  0,0,0,1,1,1,1,0,0,0
    %output(:,:,3):  0,0,0,0,0,0,0,1,1,1
    %Note that the return has the same length in the first two
    %dimensions, but has a third dimension with the length that is
    %bigger than the number of dividers by one.
    %Also note that for the easy coding of functions that calls this
    %function, this function takes only vertical X and the above example
    %is for illustration only.
    [Coor1,Coor2]=size(X);
    if Coor2==1 && Coor1>1
    else
        error('   Unexpected X dimension, use a vertical vector');
    end
    TPNo=max(size(TurningPoints));
    Sections=TPNo+1;
    TurningPoints(TPNo+1)=max(X)+1;
    LineCoefficient=zeros(Coor1,1,Sections);
    for i=1:Coor1
        LineCoefficient(i,1,find(TurningPoints>=X(i),1))=1;
    end
end