How to Delete certain rows in a Matrix

1 view (last 30 days)
I have a matrix that is 2 columns wide and 18,000 rows long. I wish to save the first 15 rows, then delete 90, save 15 more and remove 90 more rows. and so on until the matrix is completed. I am having trouble with this. should i use loops?

Accepted Answer

dpb
dpb on 6 Feb 2020
Edited: dpb on 6 Feb 2020
Use indexing expressions...
N1=15; % number to save
N2=90; % number to skip
i1=1:N1+N2:size(x,1); % locations to begin to save
i2=N1:N1+N2:size(x,1); % end locations to save
ix=cell2mat(arrayfun(@colon,i1,i2,'uni',0)); % the addressing vector of those to save
M=M(ix,:); % select the desired rows only
One can do this without the intermediaries but this makes the logic more transparent.
  2 Comments
dpb
dpb on 6 Feb 2020
No problem. BTW, for large arrays, subset selection will be quite a bit quicker than deletion I suspect altho I didn't try timing...

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!