MATLAB Answers

Nancy
4

Delete row from Matrix

Asked by Nancy
on 21 Jun 2012
Latest activity Edited by MathWorks Support Team on 9 Nov 2018
Accepted Answer by Jan
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.

7 Answers

Answer by Jan
on 22 Jun 2012
Edited by MathWorks Support Team on 9 Nov 2018
 Accepted Answer

If you have a matrix A and want to delete the 3rd and 9th rows, you can use the command:
A([3,9],:) = [];

  2 Comments

Does this use the same technique as a row deletion in a database. Because if we say we delete more than 1000 entries recursively, then should we be worried about any [primary keys.
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.

Sign in to comment.


Answer by Peter
on 30 Nov 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."
Matrix_2 = Matrix_1( [1:2,4:8,10:end] , : )
Best,
Pete

  0 Comments

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

  3 Comments

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).
Hi Mehul! It new question.
out = m1;
out(m2,:) = [];

Sign in to comment.


Answer by Andrei Bobrov
on 21 Jun 2012

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

  5 Comments

It copies the rows of m that are _not_ row 3 or 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]), :);
@ Andrei Bobrov , @ Walter Roberson,@ Jan Simson . how delete a particular row and column of a matrix by using "setdiff" . Say m= [1 2 3 4 ; 5 6 7 8; 9 10 11 12 ; 13 14 15 16 ]. i want to delete 1st row and 2nd column to obtain m=[5 7 8; 9 11 12;13 15 16]

Sign in to comment.


Answer by Alireza Rezvani on 19 Jun 2016

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

  2 Comments

Assume out is your matrix and you want to delete its first column, try this code,
out(:,1) = [];
Yes. This is the easiest way you can do.

Sign 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!

  0 Comments

Sign in to comment.


Answer by LISSA DUVVU on 29 Sep 2018

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

  1 Comment

Jan
on 7 Oct 2018
Please do not attach a new (and very vague) question in the section for answers of another questions. Such thread-hijacking is confusing only, because it is not longer clear, to which question an answer belong. Please open your own question and delete this pseudo-answer. Thanks.

Sign in to comment.