2014-07-17 38 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

相关问题