1,522 views (last 30 days)

Jan
on 22 Jun 2012

Edited: MathWorks Support Team
on 9 Nov 2018

If you have a matrix A and want to delete the 3rd and 9th rows, you can use the command:

A([3,9],:) = [];

Walter Roberson
on 6 Sep 2018

databases have their own implementations and their own tradeoffs. For example,

deletes = [3, 9];

q = ['(', strjoin(sprintfc('%d', deletes), ','), ')'];

sqlquery = ['DELETE * FROM inventoryTable WHERE productNumber in ' q];

curs = exec(conn,sqlquery);

How the database implements this is up to the database. As is typical with databases, the efficiency of this can depend upon whether productNumber is an indexed key. But that has nothing to do with MATLAB.

Katarina
on 18 Jun 2019

I used this in MATLAB R2017a and e.g.

A(3,:) = []

removes a row, but

A([3,5],:) = []

A([3,3],:) = []

A([k,k],:) = []

also removes only a row, but not a column as said above.

Is it that it doesn't work in my version of matlab of this answer needs updating?

Walter Roberson
on 18 Jun 2019

Katarina, I am not clear as to where you are seeing anything about deleting columns in this Answer?

If you want to delete a column then name it in the second index:

A(:,k) = [];

Sign in to comment.

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).

Sign in to comment.

Andrei Bobrov
on 21 Jun 2012

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

Jan
on 22 Jun 2012

SETDIFF has a remarkable overhead. ISMEMBER is smarter and twice as fast for a 100x100 matrix:

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

pradeep kumar
on 30 Aug 2014

Sign in to comment.

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) = [];

Sign in to comment.

Drew Closner
on 10 Oct 2016

Sign in to comment.

LISSA DUVVU
on 29 Sep 2018

i want to delete all columns data where the 4th row contains number 7

Jan
on 7 Oct 2018

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/41762-delete-row-from-matrix#comment_389529

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/41762-delete-row-from-matrix#comment_389529

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/41762-delete-row-from-matrix#comment_775999

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/41762-delete-row-from-matrix#comment_775999

Sign in to comment.