MATLAB Answers

0

How to remove hundreds of columns from a matrix?

Asked by Julia Williams on 20 Jun 2019
Latest activity Edited by Adam
on 20 Jun 2019
I am trying to remove the columns from my matrix. However whenever I run this code an error pops up and my matrix is not reduced. The a matrix this is referring to is imported from an excel sheet and is 1644 x 313. I am very new to Matlab and if anyone could help that would be much appreciated!
My code:
a(:, 2:16) = [];
a(:, 20:25) = [];
a(:, 29:31) = [] ;
a(:, 35:37) = [] ;
a(:, 41:43) = [] ;
a(:, 47:52) = [] ;
a(:, 56:58) = [] ;
a(:, 62:64) = [] ;
a(:, 68:70) = [] ;
a(:, 74:79) = [] ;
a(:, 83:85) = [] ;
a(:, 89:91) = [] ;
a(:, 95:97) = [] ;
a(:, 101:106) = [];
a(:, 110: 112) = [];
a(:, 116: 118) = [] ;
a(:, 122: 124) = [] ;
a(:, 128: 133) = [] ;
a(:, 137:139) = [] ;
a(:, 143: 145) = [] ;
a(:, 149: 151) = [] ;
a(:, 155: 158) = [] ;
a(:, 162: 164) = [] ;
a(:, 168: 172) = [] ;
a(:, 176: 181) = [] ;
a(:, 185: 187) = [] ;
a(:, 191: 193) = [] ;
a(:, 197: 199) = [] ;
a(:, 203: 208) = [] ;
a(:, 212: 214) = [] ;
a(:, 218: 220) = [] ;
a(:, 224: 226) = [] ;
a(:, 230: 235) = [] ;
a(:, 239: 241) = [] ;
a(:, 245: 247) = [] ;
a(:, 251: 253) = [] ;
a(:, 257: 262) = [] ;
a(:, 266: 268) = [] ;
a(:, 272: 274) = [] ;
a(:, 278: 280) = [] ;
a(:, 284: 289) = [] ;
a(:, 293: 295) = [] ;
a(:, 299: 301) = [] ;
a(:, 305: 307) = [] ;
a(:, 311: 313) = [] ;
Error that pops up in the debugger section I think:
[SL: Deleted the text of the table method disp. This is MathWorks code. If you want to show the error that you received from MathWorks code, please show the text of the error message (what's displayed in red in the Command Window) not the code that throws the error. Thanks.]

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by Adam
on 20 Jun 2019
Edited by Adam
on 20 Jun 2019
 Accepted Answer

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.

  1 Comment

Thank you Adam! You clearly explained this issue and I appreciate you taking the time to answer my question!

Sign in to comment.


Answer by Steven Lord
on 20 Jun 2019

a(:, 2:16) = [];
a(:, 20:25) = [];
Are the second batch of columns you want to delete the new columns 20 through 25 (after the original columns 2 through 16 were deleted) or the original columns 20 through 25?
If the original, delete all the columns at once.
columnsToDelete = [2:16 20:25];
a(:, columnsToDelete) = [];

  2 Comments

Thank you very much for all your help. I wanted to delete the original columns. I tried both Adam and your code and they both work perfectly. I appreciate your swift response!
Yes, concatenating all the indices you want to delete and doing it all at once is definitely preferable to simply reversing all those individual instructions. The fewer times you have to do removal operations the better, so removing e.g. 150 columns at once is much more efficient than removing one column 150 times.

Sign in to comment.