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