How to remove hundreds of columns from a matrix?
Show older comments
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.]
Accepted Answer
More Answers (1)
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
Julia Williams
on 20 Jun 2019
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.
Categories
Find more on Logical 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!