4

我有一个非常大的 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)的大小非常大(超过十亿),如果我使用循环来查找这些行,它需要很多时间。我正在寻找一种快速的代码或方法。希望它现在有意义!

4

1 回答 1

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
于 2012-09-28T00:05:01.260 回答