limiting columns of a matrix

1 view (last 30 days)
Salar
Salar on 1 Feb 2019
Edited: Bob Thompson on 1 Feb 2019
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
  2 Comments
Kevin Phung
Kevin Phung on 1 Feb 2019
can you provide an example?
Salar
Salar on 1 Feb 2019
for example:
NewPrime_CO=[1:30];
limit1=[2 3 4 5 6 7];
limit2=[15 16 17 18 19 20];
after applying the code that I wrote in previous note the result will be:
[1 8 9 10 11 12 13 14 21 22 23 24 25 26 27 28 29 30];
In the problem that I'm working on there is a 2x4000000 matrix I want to remove the elements which are between 2 limits.Some times the limits are: [] which is defined a 1x0 double in this case I get error and my code does not work.

Sign in to comment.

Accepted Answer

Bob Thompson
Bob Thompson on 1 Feb 2019
Edited: Bob Thompson on 1 Feb 2019
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) = [];

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!