Matlab:如何找到满足某些要求的矩阵的行索引? [英] Matlab: how to find row indices of a matrix satisfying certain requirements?

查看:1092
本文介绍了Matlab:如何找到满足某些要求的矩阵的行索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A是矩阵40 x 10000. V1V2V3是具有相同尺寸1 x 50的3个向量.

A is a matrix 40 x 10000. V1, V2 and V3 are 3 vectors with the same dimension 1 x 50.

我想找到三个向量W1W2W3,它们具有相同的尺寸1 X p(p:最大可能p <= 40),满足以下条件:

I want to find 3 vectors W1, W2 and W3 with the same dimension 1 X p (p: the largest possible p<=40) satisfying the following conditions:

  • W1(i),W2(i)和W3(i)属于A(i,:)
  • Wk的所有元素都属于Vk,k = 1,2,3

Wk不是已知的向量!但是Vk是预定义的.

Wk is not a known vector! but Vk are predefined.

所以目标是找到包含Wk且大小(Wk)尽可能大的A的行索引.

So the goal is to find row indices of A that contain Wk, with size(Wk) the largest possible.

示例:(我在矩阵中使用了一些字符使示例更清晰)

example: (I used some character in the matrix to make the example clearer)

      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(可能的最大值),因此:

for this example p=3 (the largest possible value) so:

W1 =[a b c]

W2 =[e f g]

W3 =[h m n]

所需结果:A(2,:),A(5,:)和A(6,:).

The desired result: A(2,:), A(5,:) and 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,所以:

for this example p=2 (the largest possible value); because 48 does not belong to V1, and 40 and 67 do not belong to V2 and V3 respectively, so:

W1 =[a c]

W2 =[e g]

W3 =[h n]

所需结果:A(2,:)和A(6,:).

The desired result: A(2,:) and A(6,:).

另一个例子:

如果:(如果A中的b是右侧的一列)

if: (if b in A is one column to the right)

      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(可能的最大值),因此:

for this example p=2 (the largest possible value), so:

W1 =[a c]

W2 =[e g]

W3 =[h n]

所需结果:A(2,:)和A(6,:).

The desired result: A(2,:) and A(6,:).

另一个例子:

如果:(如果A中的c是右侧的一列)

if: (if c in A is one column to the right)

      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(可能的最大值);所以:

for this example p=1 (the largest possible value); so:

W1 =[a]

W2 =[e]

W3 =[h]

所需结果:A(2,:).

The desired result: A(2,:).

推荐答案

以下似乎有效.变量result给出一组选定的行(例如示例中的[2 5 6][2 6]).当然,您可以选择A(result,:),或将p作为numel(result).

The following seems to work. The variable result gives the set of selected rows (such as [2 5 6] or [2 6] in your examples). Of course, you could then take A(result,:), or obtain p as 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个时,可以进行这些更改,以使代码看起来简洁-


General Case: When you have more than 3 vectors, you can make these changes, so that your code looks concise -

%// 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 ...

这篇关于Matlab:如何找到满足某些要求的矩阵的行索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆