2012-11-24 39 views
3

我有大小10000x5的矩陣格式[id, year, month, day, value]發現排具有多個屬性

例如:

[ 1 2004 1 1 100; 
    1 2004 1 2 201; 
    2 2004 1 1 30; 
    2 2004 1 2 123; 
    2 2005 1 1 300; 
    2 2005 1 2 103; 
    ...] 

我想給過濾和這個矩陣的子集複製到另一個矩陣中,搜索標準year==2004 && month==1 && day==1。所以我認爲首先要找出與給定標準匹配的向量的行索引是什麼。

首先,我試着用,

[row] = find((data(:,2) == 2004 && data(:,3) == 1 && data(:,4) == 1)); 

但它似乎並不多標準來工作,我得到的錯誤

Operands to the || and && operators must be convertible to logical scalar values. 

接下來,我試着用

key = [2004 1 1]; 
[~,index] = ismember(data,key,'rows') 

但它說

Error using ismember. A and S must have the same number of columns. 

有沒有什麼方法可以改進語法,或者有多種API搜索多個條件?

+0

哦,它的工作原理與[行] =找到((數據(:,2)== 2004年和數據(:,3)== 1的數據(:, 4)== 1)); – twfx

+0

如果您找到解決方案,您可以回答自己的問題。 – fefe

回答

2

查看建議的答案here。它提出了一種替代方法。在你的情況,你會:

key = [NaN 2004 1 1 NaN];