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

7 views (last 30 days)
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?

Accepted Answer

Star Strider
Star Strider on 18 Jan 2022
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 Comments
Image Analyst
Image Analyst on 18 Jan 2022
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 on 18 Jan 2022
@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.)

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!