Check out these implementations of Schmitt Trigger:
- Simulink library implementation of the behavioral model of Schmitt trigger: https://www.mathworks.com/help/sps/ref/schmitttrigger.html
- https://www.mathworks.com/matlabcentral/fileexchange/44419-schmitt-trigger
- https://www.mathworks.com/matlabcentral/fileexchange/56764-schmitt-trigger?s_tid=prof_contriblnk
In your implementation, if the crossings are well separated and the input isn't nosiy, you can use a logical mask and the function "find" to get the crossing points and reconstruct the output.