Search a large database

I have a very large 3D matrix, and I need to call some specific templates with a special configuration from this large matrix. For example, I need submatrices that their elements a, b, c, .., h are equal to a certain value (this is not a pattern match, but I need some patterns that have a specific value)

Is there a decision to find them very quickly? One solution, as I know, is to scan the matrix and save its template in the database, and then use the PCA to reduce the size of the database, but I'm looking for a more accurate method.

For example, suppose I have a matrix like 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] 

then I have another matrix as shown below:

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

then I have to look in "I" to find the rows, their columns 1,2 and 5 are 1,0,1 respectively (in this example, the row of 6 is the correct row. reality, the size of my matrix (I) is very large (more billion), and if I use a loop to find these strings, it takes a lot of time. I am looking for a quick code or method. I hope it will be meaningful now!

+4
source share
1 answer

If the template you are looking for is column-wise, you can do this: (If it is not more specific in your question, I will change my answer)

 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 [xyz]=ind2sub(size(A),m{i}); m{i}=[xyz]; % 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 [xyz]=ind2sub(size(A),fullMatchingIndices); finalIndices=[xyz]; % check the result if ~isempty(finalIndices) A(finalIndices(1,1)+(0:numel(p)-1),finalIndices(1,2),finalIndices(1,3)) end 

As a result, you should get a pattern p (if at least one match matches):

 ans = 2 3 4 
+1
source

Source: https://habr.com/ru/post/1436716/


All Articles