identify a matrix within a matrix

I'm pretty new to this. What I am wondering is how to test a few different matrices within a larger one.
this would be an example of what I want to do:
A = 9 7 6 5 4 A = [9 7 6 5 4;6 7 5 4 2;7 7 5 5 5;7 6 4 4 3]
6 7 5 4 2
7 7 5 5 5
7 6 4 4 3
B = 7 7 B = [7 7;7]
7
C = 7 C = [7;7;7]
7
7
D = 5 5 5 D = [5 5 5]
and then show me where in the original one they are, replacing the non-matches with 0's.
Cheers.
Any direction as to what functions I need to look into would be greatly appreciated

 Accepted Answer

Andrei Bobrov
Andrei Bobrov on 22 Sep 2016
Edited: Andrei Bobrov on 22 Sep 2016
Bad variant
A = [9 7 6 5 4;6 7 5 4 2;7 7 5 5 5;7 6 4 4 3]
B = [7 7;7 0];
p = abs(filter2(B,A) - norm(B(:))^2) < eps(1e4);
out1 = imdilate(p,B>0).*A;
C = [7;7;7];
p2 = abs(filter2(C,A) - norm(C(:))^2) < eps(1e4);
out2 = imdilate(p2,C>0).*A;
D = [5 5 5];
p3 = abs(filter2(D,A) - norm(D(:))^2) < eps(1e4);
out2 = imdilate(p3,D>0).*A;
other variant:
use m-file findarray.m:
function [idx,arrfnd] = findarray(A,B)
[m,n] = size(A);
Ai = reshape(1:n*m,[m,n]);
[mb,nb] = size(B);
B = B(:);
t = ~isnan(B);
pb = bsxfun(@plus,(0:mb-1)',(0:nb-1)*m);
pb = pb(t);
Av = reshape(Ai(1:m-mb+1,1:n-nb+1),1,[]);
i0 = bsxfun(@plus,Av,pb(:));
ii = all(bsxfun(@eq,A(i0),B(t)));
idx = i0(:,ii);
arrfnd = zeros(m,n);
arrfnd(idx) = A(idx);
end
example of use
>> A
A =
9 7 6 5 4
6 7 5 4 2
7 7 5 5 5
7 6 4 4 3
>> B1
B1 =
7 7
7 NaN
>> [idx,arrfnd] = findarray(A,B1)
idx =
3
4
7
arrfnd =
0 0 0 0 0
0 0 0 0 0
7 7 0 0 0
7 0 0 0 0
>> C
C =
7
7
7
>> [idx,arrfnd] = findarray(A,C)
idx =
5
6
7
arrfnd =
0 7 0 0 0
0 7 0 0 0
0 7 0 0 0
0 0 0 0 0
>> D
D =
5 5 5
>> [idx,arrfnd] = findarray(A,D)
idx =
11
15
19
arrfnd =
0 0 0 0 0
0 0 0 0 0
0 0 5 5 5
0 0 0 0 0
>>|

8 Comments

This worked great thanks, I changed the matrix A to incorporate some more of the patterns and it showed them as well. I am new to this software and am wondering if you could explain how you got to this code, if you have time? I am curious as to how you put it together. I have been pulling it apart to find out how it works, but still a bit puzzled. Cheers, Byron.
Hi! Byron! What part of the code is not clear?
p = abs(filter2(B,A) - norm(B(:))^2) < eps(1e4);
out1 = imdilate(p,B>0).*A;
As previously stated, I am fairly new, I've followed through some an introduction course on Coursera.org but I'm still left without a great knowledge. Just wondering if you could break it down, again if you've got the time. Cheers.
The single quote is the complex conjugate transpose operator:
The function bsxfun is used here to add the row (0:nb-1)*m to the column (0:mb-1)', creating a matrix. You can try it on some simple example to see what it is doing:
>> bsxfun(@plus,(0:3)',0:5)
ans =
0 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
Satuk Bugrahan
Satuk Bugrahan on 22 Sep 2016
Edited: Satuk Bugrahan on 22 Sep 2016
Thank you for this clear answer, though I just deleted my message because understand the deal myself howewer I am still a little bit confused about how did we add two matrix with different sizes together with 'bsxfun' . When I look at the input matrices still I cant estimate the output . At any case , thank you for the answer ...
Hi Byron! Coursera.org is very good but I would started with MATLAB Documentation.
Please first read about functions used in these expressions:
imdilate from the Image Processing Toolbox.
Hi Andrei!
Your code works perfectly, but I want to use it in a 3D Matrix. Unfortunately your Code only works in 2D cause bsxfun respectivly the plus operator only works in 2D with this result. Do you know a way to expand this function that it would work in 3D.
Cause Im writing a recursiv function with up to 300Mio iteratons the runtime is very important.
Thanks!
I solved the problem with the following code. This works in 3D Matrix.
[m,n,o] = size(A);
Ai = reshape(1:n*m*o,[m,n,o]);
[mb,nb,ob] = size(B);
B = B(:);
t = ~isnan(B);
pb = (0:mb-1)'+(0:nb-1)*m+reshape([0:ob-1],1,1,ob)*n*m;
pb = pb(t);
Av = reshape(Ai(1:m-mb+1,1:n-nb+1,1:o-ob+1),1,[]);
i0 = Av+pb(:);
ii = all(bsxfun(@eq,A(i0),B(t)));
idx = i0(:,ii);

Sign in to comment.

More Answers (2)

Note that B = [7 7;7] is not a matrix.
You can find your required numbers from matrix A using find or ==.
Eg:
idx = A==7 ;
A(idx)
idx = find(A==5)
A(idx)

Products

Asked:

on 22 Sep 2016

Edited:

on 16 Dec 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!