How to extract specific row data from a big matrix?

Here are my data [1 5; 1 1; 1 2; 1 3; 1 1; 2 2; 2 2; 2 1; 2 3; 2 1] . Now, I want to extract the rows which have either 1 1, 1 2 , 2 1, or 2 2 and export it as a new data file. Any pointers on how to achieve this? I am able to extract by just one row value. For example, with the below code I get all the data that has 2 in the column 1. However, I do not know how to include both columns for all the above-mentioned cases i.e. 1 1, 1 2, 2 1, 2 2.
Code: data=[1 5; 1 1; 1 2; 1 3; 1 1; 2 2; 2 2; 2 1; 2 3; 2 1]; temp=data(data(:,1)==2,:);
Output: temp =
2 2
2 2
2 1
2 3
2 1

 Accepted Answer

Try this:
data = [1 5; 1 1; 1 2; 1 3; 1 1; 2 2; 2 2; 2 1; 2 3; 2 1];
ptrn = [1 1; 1 2; 2 1; 2 2]; % Patterns Matrix
Lidx = ismember(data, ptrn, 'rows'); % Logical Index Vector For Rows
Out = data(Lidx,:)
Out =
1 1
1 2
1 1
2 2
2 2
2 1
2 1
Experiment with the ismember (link) function.

More Answers (0)

Asked:

Aks
on 28 Jun 2018

Commented:

on 29 Jun 2018

Community Treasure Hunt

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

Start Hunting!