Imperfection-enabled memristive switching in van der Waals materials