2014-07-17 37 views
2

A是矩陣40 x 10000V1V2V3是具有相同維數的3個向量1 x 50Matlab:如何找到滿足一定要求的矩陣的行索引?

我想找到3個矢量W1W2W3具有相同尺寸1 X p(P:最大可能的p < = 40)滿足以下條件:

  • W1(i)中,W2(ⅰ )和W3(ⅰ)屬於A(I,:)
  • 所有WK的元素屬於VK中,k = 1,2,3

WK是不是已知的矢量!但Vk是預定義的。

因此,我們的目標是找到包含Wk的A的行索引,其大小(Wk)最大。

例如:(I使用的一些字在基質中以使示例更清楚)

 95 45 92 41 13  1 84 
     a  1  e 89  h 74 52 
A= 60 82 17  5 19 44 20 
     48 44 40 35 67 93 67 
     b 61  f 81  m 46 83 
     c 79  g 20  n 41  1 

V1 = [51 a 23 11 b 5 c] 

V2 = [e g 93 14 22 f 10] 

V3 = [81 n 87 h 45 77 m] 

這個例子p=3(最大可能值),以便:

W1 =[a b c] 

W2 =[e f g] 

W3 =[h m n] 

期望的結果: A(2,:),A(5,:)和A(6,:)。

另一個例子:

如果:

 95 45 92 41 13  1 84 
     a  1  e 89  h 74 52 
A= 60 82 17  5 19 44 20 
     b 44 40 35 67 93 67 
     48 61  f 81  m 46 83 
     c 79  g 20  n 41  1 

V1 = [51 a 23 11 b 5 c] 

V2 = [e g 93 14 22 f 10] 

V3 = [81 n 87 h 45 77 m] 

這個例子p=2(最大可能值);因爲48不屬於V1,和40和67分別不屬於V2和V3,所以:

W1 =[a c] 

W2 =[e g] 

W3 =[h n] 

期望的結果:A(2,:)和A(6,:)。

另一個例子:

,如果:(如果B在A中是一列到右側)

 95 45 92 41 13  1 84 
     a  1  e 89  h 74 52 
A= 60 82 17  5 19 44 20 
     77  b 40 35 67 93 67 
     48 61  f 81  m 46 83 
     c 79  g 20  n 41  1 

V1 = [51 a 23 11 b 5 c] 

V2 = [e g 93 14 22 f 10] 

V3 = [81 n 87 h 45 77 m] 

這個例子p=2(最大可能值),所以:

W1 =[a c] 

W2 =[e g] 

W3 =[h n] 

期望的結果:A(2,:)和A(6,:)。

另一個例子:

,如果:(如果c中,A是一列到右側)

 95 45 92 41 13  1 84 
     a  1  e 89  h 74 52 
A= 60 82 17  5 19 44 20 
     b 44 40 35 67 93 67 
     48 61  f 81  m 46 83 
     88  c  g 20  n 41  1 

V1 = [51 a 23 11 b 5 c] 

V2 = [e g 93 14 22 f 10] 

V3 = [81 n 87 h 45 77 m] 

這個例子p=1(最大可能值);所以:

W1 =[a] 

W2 =[e] 

W3 =[h] 

期望的結果:A(2,:)。

+0

還能有重複,比如'W1 = [ abca]'等等? – Divakar

+0

@Divakar:不,並且p不是預定義的。 – bzak

+0

我們可以將矢量(3)的數量固定嗎?或者應該是一個可變參數(更難)? –

回答

3

以下似乎工作。變量result給出了選定行的集合(例如您的示例中的[2 5 6][2 6])。當然,你可以拿A(result,:),或者p作爲numel(result)

eq1 = any(bsxfun(@eq, A, permute(V1, [1 3 2])), 3); %// does entry of A match V1? 
eq2 = any(bsxfun(@eq, A, permute(V2, [1 3 2])), 3); %// ...V2? 
eq3 = any(bsxfun(@eq, A, permute(V3, [1 3 2])), 3); %// ...V3? 
result = []; 
for nr = 1:size(A,1) %// try all numbers of rows, in ascending order 
    rows = nchoosek(1:size(A,1),nr).'; %'// all combinations of that nr rows 
    for rr = rows %// try each combination of nr rows 
     if any(all(eq1(rr,:),1)) & any(all(eq2(rr,:),1)) & any(all(eq3(rr,:),1)) 
      %// ",1" needed to make "all" by columns even if there's only one row 
      result = rr; %// (overw)rite result (nr is larger now) 
      break %// no need to keep trying combinations of rows for this nr 
     end 
    end 
end 

一般情況:當你有3層以上的載體,可以使這些變化,使你的代碼看起來簡潔 -

%// Concatenate all V vectors into one 
V = cat(1,V1,V2,V3,V4,V5,...) 

%// Replace eq1, eq2 and eq3 calculations with this single calculation 
eqa = squeeze(any(bsxfun(@eq,A,permute(V,[4 3 2 1])),3)); 

%// Replace the `IF` part with - 
if all(any(all(eqa(rr,:,:),1)),3), result = rr, break, end ... 
+0

非常感謝您的回答!訂單[1 3 2]是否重要?如果V是1x50會發生什麼? – bzak

+1

「'[1 3 2]'」是尺寸(1:行,2:列,3:切片),而不是矢量的條目。它適用於任何行向量(即大小爲1 x的任何) –

+0

任何(bsxfun(@eq,A,置換(V1,...)),3)中的3?我爲我的無能表示歉意! – bzak

相關問題