How to filter rows?
Afficher commentaires plus anciens
I have a matrix like this
1 2 3 4 5 6 7 8
2 3 4 1 2 3 4 5
1 2 3 4 1 1 1 1
1 2 3 5 4 2 2 1
I want to separate those rows having first four entries as 1 2 3 4, as in above case I separate out row 1 and row three.
Réponse acceptée
Plus de réponses (1)
Guillaume
le 9 Fév 2018
%R2016b or later:
tokeep = all(A(:, 1:4) == [1 2 3 4], 2)
%earlier versions
tokeep = all(bsxfun(@eq, A(:, 1:4), [1 2 3 4]), 2)
%then
A(tokeep, :)
4 commentaires
Les Beckham
le 21 Fév 2023
This question was about extracting elements from a matrix, not from a table containing cell arrays. You should start a new question and provide example data and what you want to do with the data, including any code you have tried (along with the results you expect and what you are getting instead, if applicable).
I suggest reading this first: TUTORIAL: how to ask a question (on Answers) and get a fast answer
Kurt
le 22 Fév 2023
The question is simply "How to filter rows?"
Anyway, I found the answer I needed. To filter rows of cells (not numerics) you can either read your data in as a table or convert your matrix to a table. Then use this one-liner:
output_data = input_data(table2array(input_data(:,col)) == "pattern",:);
where "col" is the column number you are filtering on.
There may be other approaches, but this works for me.
Les Beckham
le 22 Fév 2023
I'm glad you figured it out.
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!