Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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]
- 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
- ans =
- 2
- 3
- 4
- C = convn(data,pattern,'same');
Add Comment
Please, Sign In to add comment