First thing that comes to mind, which may or may not be the only problem:
Never delete parts of an array from the front in a loop (or equivalent)
You are basically pulling the rug out from under your feet. Just take your first two lines of code and imagine, conceptually, what is happening:
You delete columns 2 to 16. That's fine, they disappear and now your matrix has 15 fewer columns. What was column index 17 is now index 2. So now we come to your second line and you try to delete columns 20 to 25. This will succeed, but is not deleting what you think it is (unless in the unlikely event you have worked all this out and it is deliberate). What is now column 20 was previously column 35 and the column 20 you presumably want to delete has become column 5 due to the previous deletions.
This carries on down the instruction list and in some cases may appear like it worked, but will have deleted totally the wrong columns.
In most cases you will come to an instruction that says, for example delete column 200, which was valid initially when you had 250 columns, but you've since deleted 100 of them so now your largest column index is 150. (Note, those numbers are just for example and not related to your exact case, but the theory is the same - I can't be bothered to work out at exactly which instruction yours hits the problem - it doesn't matter anyway as it won't help fix it). So at that point it will fall over because your matrix has been shrinking every time you delete something.
The quickest way to solve that is reverse all your instructions and delete from the end instead, though it's still a bit ugly it at least won't run into this issue.
Best Answer