Asked by Nancy
on 21 Jun 2012

I have a Matrix of 400 rows and 40 columns. I want to completely remove rows 3 and 9 to get a matrix with 398 rows. How can I do it in MATLAB.

Answer by Jan
on 22 Jun 2012

Edited by Jan
on 16 Apr 2016

Accepted Answer

Timings:

x = rand(100, 100); tic; for i = 1:1e4 y = x; y([3, 9], :) = []; clear('y'); % distract the JIT end toc % Elapsed time is 0.594352 seconds

tic; for i = 1:1e4 y = x; y(9, :) = []; y(3, :) = []; clear('y'); % distract the JIT end toc % Elapsed time is 0.783146 seconds, two data copies

tic; for i = 1:1e4 y = x(setdiff(1:size(x,1), [3,9]), :); clear('y'); % distract the JIT end toc % Elapsed time is 1.892520 seconds, SETDIFF overhead and numerical indexing

tic; for i = 1:1e4 y = x(~ismember(1:size(x,1), [3,9]), :); clear('y'); % distract the JIT end toc % Elapsed time is 1.081171 seconds, faster logical indexing

tic; for i = 1:1e4 index = true(1, size(x, 1)); index([3, 9]) = false; y = x(index, :); clear('y'); % distract the JIT end toc % Elapsed time is 0.379877 seconds

Measured under Matlab2009a/64, Win7 => Advantage for the logical indexing method.

Timings for 2011b/64, Win7:

Elapsed time is 0.481238 seconds. Elapsed time is 0.669098 seconds. Elapsed time is 1.909885 seconds. Elapsed time is 0.838608 seconds. Elapsed time is 0.304096 seconds.

[EDITED] Timings for R2015b, same machine:

Elapsed time is 0.599108 seconds. slower Elapsed time is 0.873113 seconds. slower Elapsed time is 2.192391 seconds. slower Elapsed time is 0.734789 seconds. faster Elapsed time is 0.487023 seconds. slower

Log in to comment.

Answer by Dan W
on 23 Jan 2015

I'm not sure if this is new syntax or not, but it works with R2012a and it's fast and simple.

x = rand(100); tic; x([3,9],:) = []; toc; % Elapsed time is 0.000230 seconds.

Mehul Agrawal
on 10 Jun 2016

Thanks, this works pretty easy. But I have a problem where the elements to remove are decided dynamically. So, I have a matrix m1 of size 100 X 100. And another matrix m2 of size 10X1. m2 has the row number to remove from m1 (they are not in any order). What is the best way to do this ?

Eg: m1 = rand(100); m2 = [1,6,4,8,10]; (this is the output of another function call).

Andrei Bobrov
on 10 Jun 2016

Hi Mehul! It new question.

Andrei Bobrov
on 10 Jun 2016

out = m1; out(m2,:) = [];

Log in to comment.

Answer by Andrei Bobrov
on 21 Jun 2012

m = m(setdiff(1:size(m,1),[3,9]),:);

Walter Roberson
on 21 Jun 2012

It copies the rows of m that are _not_ row 3 or 9.

Jan
on 22 Jun 2012

m = m(~ismember(1:size(m, 1), [3,9]), :);

pradeep kumar
on 30 Aug 2014

Log in to comment.

Answer by Alireza Rezvani
on 19 Jun 2016

sry, how i can Deleting individual columns of a matrix, any body know?

Muhammad Usman Saleem
on 19 Jun 2016

Assume out is your matrix and you want to delete its first column, try this code,

out(:,1) = [];

Log in to comment.

Answer by Drew Closner
on 10 Oct 2016

So I have to make a function that is able to delete a row in a matrix [I have 3 by 3]. I am not sure how to do this, please leave any help you can!

Log in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/41762-delete-row-from-matrix#comment_389529

See also http://www.mathworks.com/matlabcentral/answers/105768-how-can-i-delete-certain-rows-of-a-matrix-based-on-specific-column-values#answer_226615

Log in to comment.