№ 1
[val,ind] = max(bsxfun(@eq,permute(M,[4 2 1 3]),permute(N,[2 3 4 1])),[],2)
matches = squeeze(all(diff(ind,1)>0,1).*all(val,1))
out1 = any(matches,2) %// Solution - 1
out2 = sum(matches,1) %// Solution - 2
# 2
, permuting N longish N -
[val,ind] = max(bsxfun(@eq,N,permute(M,[3 4 1 2])),[],4)
matches = squeeze(all(diff(ind,[],2)>0,2).*all(val,2))
out1 = any(matches,1) %// Solution - 1
out2 = sum(matches,2) %// Solution - 2
№ 3
-scroogey -
out1 = false(size(M,1),1); %// Storage for Solution - 1
out2 = zeros(size(N,1),1); %// Storage for Solution - 2
for k=1:size(N,1)
[val3,ind3] = max(bsxfun(@eq,N(k,:),permute(M,[1 3 2])),[],3);
matches = all(diff(ind3,[],2)>0,2).*all(val3,2);
out1 = or(out1,matches);
out2(k) = sum(matches);
end
№ 4
-scroogey GPU -
gM = gpuArray(M);
gN = gpuArray(N);
gout1 = false(size(gM,1),1,'gpuArray'); %// GPU Storage for Solution - 1
gout2 = zeros(size(gN,1),1,'gpuArray'); %// GPU Storage for Solution - 2
for k=1:size(gN,1)
[val3,ind3] = max(bsxfun(@eq,gN(k,:),permute(gM,[1 3 2])),[],3);
matches = all(diff(ind3,[],2)>0,2).*all(val3,2);
gout1 = or(gout1,matches);
gout2(k) = sum(matches);
end
out1 = gather(gout1); %// Solution - 1
out2 = gather(gout2); %// Solution - 2
GPU . M : 320000X5 N : 2100X3 ( , ), . GTX 750 Ti 13.867873 seconds!! , GPU , .
№ 5
--scroogey GPU -
gM = gpuArray(M);
gN = gpuArray(N);
gout1 = false(size(gM,1),1,'gpuArray'); %// GPU Storage for Solution - 1
gout2 = zeros(size(gN,1),1,'gpuArray'); %// GPU Storage for Solution - 2
for k=1:size(gN,1)
[val2,ind2] = max(bsxfun(@eq,gM,permute(gN(k,:),[1 3 2])),[],2);
matches = all(diff(ind2,[],3)>0,3).*all(val2,3);
gout1 = or(gout1,matches);
gout2(k) = sum(matches);
end
out1 = gather(gout1); %// Solution - 1
out2 = gather(gout2); %// Solution - 2