MATLAB: How to Delete certain rows in a Matrix

columnMATLABmatrix

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?

Best Answer

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.