identify a matrix within a matrix

7 vues (au cours des 30 derniers jours)
byron goodship
byron goodship le 22 Sep 2016
Modifié(e) : Arne T le 16 Déc 2020
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

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 22 Sep 2016
Modifié(e) : Andrei Bobrov le 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 commentaires
Arne T
Arne T le 15 Déc 2020
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!
Arne T
Arne T le 16 Déc 2020
Modifié(e) : Arne T le 16 Déc 2020
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);

Connectez-vous pour commenter.

Plus de réponses (2)

Matt J
Matt J le 22 Sep 2016

KSSV
KSSV le 22 Sep 2016
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)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by