Deleting specific rows in a matrix

I want to delete rows that contain element of value 0
for example in the following matrix B
row 3, 4 ,5 contains a zero valued element. So I want to delete these rows and the resultant matrix would be 2*3 .
How can I remove these particular rows
120 150 200
250 300 350
420 450 0
0 100 400
450 0 420

 采纳的回答

A=[120 150 200
250 300 350
420 450 0
0 100 400
450 0 420]
index=find(nanmean(A==0,2))
A(index,:)=[]
Below is the output of index and A (after removing the rows having zero value)
index =
3
4
5
A =
120 150 200
250 300 350

4 个评论

You asked in an another comment the reason behind using 2 in the line second line of code. Here is the reason just for your clarification:
2 is used because we are considering the second dimension of a matrix.
If you wish to do the same calucluation along columns, you can put 1 in place of 2 to get the column index having zero values in any of the available rows.
Hope this helps.
ANKUR KUMAR I want to know why 2 is used in index=find(nanmean(A==0,2))

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

产品

版本

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by