How do I crop a matrix in MATLAB based on contents of one column?

5 次查看(过去 30 天)
I have a table saved as matrix with 17 rows and 19 colums. From here I want to crop it to a matrix to keep only those rows which have non-empty 5th column. I want to get rid of all other rows. Can someone please provide insight?

采纳的回答

Star Strider
Star Strider 2022-1-18
I am not certain what the matrix is, or whether there are other missing entries in the matrix. If only the 5th column has missing entries, the rmmissing function may be the most efficient way of doing this.
Otherwise, using ‘logical indexing’ would be best —
Lv = ~ismissing(M(:,5));
M_new = M(Lv,:);
where ‘M’ is the matrix.
.
  4 个评论
Image Analyst
Image Analyst 2022-1-18
Yeah, since matrices cannot be empty, but (I think) table variables can. However you can check for a matrix value like 0 or 999 or some range of values, like negative values or something.
@Anjali Mishra since you said it works, please "Accept this Answer". 🙂
Star Strider
Star Strider 2022-1-18
@Image Analyst Thank you!
All table variables have to have the same number of rows, so deleting a single entry in any one row will throw an error. However cell arrays can have empty cells, and table arrays with cell array variables can have empty cells. (At least that’s my experience.)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by