Reputation: 3744
For example, say I have a 3-column matrix of news notifications, where the first column gives the serial number of news read in a sequence, the second column givess the Category of the news, and the third column gives whether the notification was opened or not (binary, 1 for read or 0 for not read). So, an excerpt might look like this:
1 12 1
2 13 0
3 13 1
4 12 0
5 14 1
6 13 0
7 12 0
8 13 1
9 14 0
10 12 1
And I want all the rows where notifications from category 12 were read. So, the output should be this:
1 12 1
10 12 1
So, if the data matrix is called input
, and the resultant matrix I want is named output
, I would write the following code:
for i=1:size(input,1)
temp = input(input(:,2)==12,:);
output = temp(temp(:,3)==1,:);
end
But I suppose this should a trivial thing to do in MATLAB. So, is there a one liner that does this?
Upvotes: 2
Views: 70
Reputation: 25232
Here is a solution using the supposed to be fast in-built function ismember
, which is actually the dedicated function for what you want to achieve:
%// data
A =[1 12 1
2 13 0
3 13 1
4 12 0
5 14 1
6 13 0
7 12 0
8 13 1
9 14 0
10 12 1]
%// sequence to search for
x = [ 12 1 ]
%// filtering
out = A( ismember(A(:,2:3),x,'rows'), : )
out =
1 12 1
10 12 1
Upvotes: 0