Reputation: 563
A
is a matrix 40 x 10000
. V1
, V2
and V3
are 3 vectors with the same dimension 1 x 50
.
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:
Wk is not a known vector! but Vk are predefined.
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]
for this example p=3
(the largest possible value) so:
W1 =[a b c]
W2 =[e f g]
W3 =[h m n]
The desired result: A(2,:), A(5,:) and A(6,:).
another example:
if:
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]
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]
The desired result: A(2,:) and A(6,:).
another example:
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]
for this example p=2
(the largest possible value), so:
W1 =[a c]
W2 =[e g]
W3 =[h n]
The desired result: A(2,:) and A(6,:).
another example:
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]
for this example p=1
(the largest possible value); so:
W1 =[a]
W2 =[e]
W3 =[h]
The desired result: A(2,:).
Upvotes: 2
Views: 253
Reputation: 112769
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
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 ...
Upvotes: 3