MATLAB: Limiting columns of a matrix

column limitingmatrix

I'm trying to have limitation over the columns of a matrix. The limits are set automatically during running the code and the values of limits are indices of columns. Some times the value of the limits are [] and when I run the following code I get this error:Index exceeds array bounds.
here is the code:NewPrime_CO=NewPrime_CO(: , [1:limit1(1)-1 limit1(end)+1:limit2(1) limit2(end)+1:end]);
I can't find out what to do to solve it. Thanks in advance

Best Answer

The reason you're getting that error is because you cannot call the index of an empty array. If limit1 is (1x0) double and you attempt to call limit1(1) you will receive this error. Although the variable technically has a row dimension, there is no actual element to call, so calling any element exceeds the matrix dimensions. I would suggest working around this by concatonating all of your limits together and entering them as one array.
limits = [limit1 limit2];
NewPrime_CO(:,limits) = [];