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或0没有读过)。所以,一段摘录可能是这样的:
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:
我要读取第12类通知的所有行。因此,输出应该是:
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?
但我想这在MATLAB中应该很简单。有这样的内线吗?
2 个解决方案
#1
4
output=input(find(input(:,2)==12 & input(:,3)==1),:)
#2
0
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:
这里有一个解决方案,使用所谓的快速内置函数ismember,它实际上是你想要实现的目标的专用函数:
%// 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
#1
4
output=input(find(input(:,2)==12 & input(:,3)==1),:)
#2
0
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:
这里有一个解决方案,使用所谓的快速内置函数ismember,它实际上是你想要实现的目标的专用函数:
%// 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