identify a matrix within a matrix

조회 수: 7 (최근 30일)
byron goodship
byron goodship 2016년 9월 22일
편집: Arne T 2020년 12월 16일
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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 9월 22일
편집: Andrei Bobrov 2016년 9월 22일
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
Arne T
Arne T 2020년 12월 15일
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 2020년 12월 16일
편집: Arne T 2020년 12월 16일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Matt J
Matt J 2016년 9월 22일

KSSV
KSSV 2016년 9월 22일
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)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by