【问题标题】:searching on a large database在大型数据库上搜索
【发布时间】:2012-09-19 20:08:43
【问题描述】:

我有一个非常大的 3D 矩阵,我需要从这个大矩阵中调用一些具有特殊配置的特定模式。例如,我需要一个子矩阵,它们的 a,b,c,..,h 元素与特定值相等(它不是模式匹配,但我需要一些具有特定值的模式)

有没有什么办法可以快速找到它们?据我所知,一种解决方案是扫描矩阵并将其模式存储在数据库中,然后使用 PCA 来减小数据库的大小,但我正在寻找一种更准确的方法。

例如,假设我有一个矩阵,例如 I:

    I=[1    0   0   1   0   1   0   1   0
       0    0   0   0   0   0   0   0   0
       0    1   0   0   1   0   0   1   0
       1    0   1   1   0   1   0   1   0
       0    0   0   0   0   0   0   0   0
       1    0   0   0   1   0   1   0   0
       0    0   0   0   0   0   0   0   0
       1    0   1   0   0   0   0   0   1
       0    0   1   0   0   0   0   1   0]

然后我有另一个矩阵如下:

    p=[1    0   NaN NaN 1   NaN NaN NaN NaN]

然后,我应该在“I”中查找其第 1,2 和 5 列分别等于 1,0,1 的行(在此示例中,第 6 行是正确的行。但是,在实际上,我的矩阵(I)的大小非常大(超过十亿),如果我使用循环来查找这些行,它需要很多时间。我正在寻找一种快速的代码或方法。希望它有意义现在!

【问题讨论】:

  • 我编辑了这个问题,我认为它现在很好。

标签: image matlab pattern-matching image-recognition


【解决方案1】:

如果您要查找的模式是按列排列的,您可以执行以下操作: (如果不是,请在您的问题中更具体,我会修改我的答案)

A=randi(10,[11 12 13]); %%% your matrix
p=[2;3;4]; %%% your pattern

m=cell(1,numel(p));
% for each element of the pattern
for i=1:numel(p)
  % find the element of A matching the i-th element of p
  m{i}=find(A==p(i));
  % convert to indices
  [x y z]=ind2sub(size(A),m{i});
  m{i}=[x y z];
  % convert to the position of the first element of the pattern
  m{i}(:,1)=m{i}(:,1)-(i-1);
  % remove when it goes below zero
  m{i}(m{i}(:,1)<=0,:)=[];
end

% accumulate everything to find out where all the elements of the pattern match
numberOfMatching=accumarray(vertcat(m{:}),1,size(A)); 
fullMatchingIndices=find(numberOfMatching==numel(p));
% convert to sub=indices
[x y z]=ind2sub(size(A),fullMatchingIndices);
finalIndices=[x y z];

% check the result
if ~isempty(finalIndices)
  A(finalIndices(1,1)+(0:numel(p)-1),finalIndices(1,2),finalIndices(1,3))
end

因此,您应该获得模式 p(如果找到至少一个匹配项):

ans =
     2
     3
     4

【讨论】:

  • Oli,你知道了,但是我的矩阵非常非常大(200x200x200)!而且在每次搜索时,我都需要检查大约 10-20 个数据。所以,问题在于它的 CPU 时间。 1)对于一旦资助具有p的模式就跳过循环的情况,如何更改代码? 2)没有pattern的时候怎么改,然后选择最后一个匹配的?我的意思是,在 p 中删除一个元素并找到模式。但是,它不应该再次重复循环,并且应该具有最新建立的模式!
  • 在我的电脑上,对于大小为 [200 200 200] 的矩阵,上述代码可在 1 秒内工作。 2)如果没有模式,你怎么能匹配?你也想要近似匹配吗?
  • 我只需要找到具有这些值的模式。考虑我扫描模板大小为 T(假设为 20x20)的大矩阵 (G),并将所有模式存储在数据库中。因此,得到的数据库会有很多模式(行)和 20^2 列。所以,我想找到模式,例如,它们的第 3、7、8、15 个元素等于某些特定值。是我的问题!我认为应该更改代码,是吗?
猜你喜欢
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-24
  • 1970-01-01
相关资源
最近更新 更多