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.

3 件のコメント

Yuvraj Praveen Soni
Yuvraj Praveen Soni 2019 年 12 月 9 日
if assume A is your matrix with 400 rows and 40 column,
To remove 3rd row
a(3,:)=[];
now your 9th row become 8th row;
a(8,:)=[];
and with this your 3rd and 9th row will be deleted, you can also check the size by
size(a);
Thank You
Gokul Krishna N
Gokul Krishna N 2020 年 11 月 2 日
Thanks

サインインしてコメントする。

 採用された回答

Jan
Jan 2012 年 6 月 22 日
編集済み: MathWorks Support Team 2018 年 11 月 9 日

46 投票

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

5 件のコメント

Srikanth
Srikanth 2018 年 8 月 9 日
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.
Walter Roberson
Walter Roberson 2018 年 9 月 6 日
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
Katarina 2019 年 6 月 18 日
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
Walter Roberson 2019 年 6 月 18 日
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) = [];
Arezoo Samiei
Arezoo Samiei 2021 年 10 月 7 日
Thank you so much Jan!

サインインしてコメントする。

その他の回答 (5 件)

Peter
Peter 2012 年 11 月 30 日

32 投票

"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

1 件のコメント

muhammad usama
muhammad usama 2023 年 5 月 7 日
Thank you, the simplest one.

サインインしてコメントする。

Dan W
Dan W 2015 年 1 月 23 日

13 投票

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 件のコメント

Mehul Agrawal
Mehul Agrawal 2016 年 6 月 10 日
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
Andrei Bobrov 2016 年 6 月 10 日
Hi Mehul! It new question.
Andrei Bobrov
Andrei Bobrov 2016 年 6 月 10 日
out = m1;
out(m2,:) = [];

サインインしてコメントする。

Andrei Bobrov
Andrei Bobrov 2012 年 6 月 21 日

6 投票

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

5 件のコメント

Sean de Wolski
Sean de Wolski 2012 年 6 月 21 日
mmm glass half full
Ryan
Ryan 2012 年 6 月 21 日
Anyone care to explain this one?
Walter Roberson
Walter Roberson 2012 年 6 月 21 日
It copies the rows of m that are _not_ row 3 or 9.
Jan
Jan 2012 年 6 月 22 日
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
pradeep kumar 2014 年 8 月 30 日
@ 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]

サインインしてコメントする。

Alireza Rezvani
Alireza Rezvani 2016 年 6 月 19 日

5 投票

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

2 件のコメント

Muhammad Usman Saleem
Muhammad Usman Saleem 2016 年 6 月 19 日
Assume out is your matrix and you want to delete its first column, try this code,
out(:,1) = [];
surendra bala
surendra bala 2018 年 3 月 31 日
Yes. This is the easiest way you can do.

サインインしてコメントする。

LISSA DUVVU
LISSA DUVVU 2018 年 9 月 29 日

1 投票

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

1 件のコメント

Jan
Jan 2018 年 10 月 7 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

質問済み:

2012 年 6 月 21 日

コメント済み:

2023 年 5 月 7 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by