Guest User

Untitled

a guest
Jan 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. I=[1 0 0 1 0 1 0 1 0
  2. 0 0 0 0 0 0 0 0 0
  3. 0 1 0 0 1 0 0 1 0
  4. 1 0 1 1 0 1 0 1 0
  5. 0 0 0 0 0 0 0 0 0
  6. 1 0 0 0 1 0 1 0 0
  7. 0 0 0 0 0 0 0 0 0
  8. 1 0 1 0 0 0 0 0 1
  9. 0 0 1 0 0 0 0 1 0]
  10.  
  11. p=[1 0 NaN NaN 1 NaN NaN NaN NaN]
  12.  
  13. A=randi(10,[11 12 13]); %%% your matrix
  14. p=[2;3;4]; %%% your pattern
  15.  
  16. m=cell(1,numel(p));
  17. % for each element of the pattern
  18. for i=1:numel(p)
  19. % find the element of A matching the i-th element of p
  20. m{i}=find(A==p(i));
  21. % convert to indices
  22. [x y z]=ind2sub(size(A),m{i});
  23. m{i}=[x y z];
  24. % convert to the position of the first element of the pattern
  25. m{i}(:,1)=m{i}(:,1)-(i-1);
  26. % remove when it goes below zero
  27. m{i}(m{i}(:,1)<=0,:)=[];
  28. end
  29.  
  30. % accumulate everything to find out where all the elements of the pattern match
  31. numberOfMatching=accumarray(vertcat(m{:}),1,size(A));
  32. fullMatchingIndices=find(numberOfMatching==numel(p));
  33. % convert to sub=indices
  34. [x y z]=ind2sub(size(A),fullMatchingIndices);
  35. finalIndices=[x y z];
  36.  
  37. % check the result
  38. if ~isempty(finalIndices)
  39. A(finalIndices(1,1)+(0:numel(p)-1),finalIndices(1,2),finalIndices(1,3))
  40. end
  41.  
  42. ans =
  43. 2
  44. 3
  45. 4
  46.  
  47. C = convn(data,pattern,'same');
Add Comment
Please, Sign In to add comment