How to compare two matrices of diffrent dimensions by looping.

9 次查看(过去 30 天)
Basically, i want to scan a small matrix over bigger matrix and then compare it's elements one by one for greater or less than bigger matrix.The comparison can be done in either way through column or through row.
  3 个评论
Sonny Sood
Sonny Sood 2013-7-28
the small matrix should be compared with that of big matrix for greater than or equal to. The result is to find a match between the elements of two matrix's and store their positions(index) after being compared. The result i want is; 1=1,match found,store it 5>2,store it's position and the element 4>3,store it 0<4,neglect it
Jan
Jan 2013-7-28
@Sonny: Please answer Azzi's question. Do not describe how you want to create the output but post manually created results. Such unequivocal examples are very useful. I do not understand the explanation "1=1,match found,store it 5>2,store it's position and the element 4>3,store it 0<4,neglect it". Neither "store it" not "neglect it" reveal any details about the wanted result.

请先登录,再进行评论。

回答(4 个)

per isakson
per isakson 2013-7-27
编辑:per isakson 2013-7-28
Not by looping, however try
a=[1,2;3,4];
b=[1,5,6;4,0,8;11,12,0];
sza = size( a );
is_greater_equal = ( a >= b(1:sza(1),1:sza(2)) )
it returns
is_greater_equal =
1 0
0 1
and to find the indicies
>> [ixr,ixc]=find(is_greater_equal)
ixr =
1
2
ixc =
1
2
  2 个评论
Sonny Sood
Sonny Sood 2013-7-28
编辑:Sonny Sood 2013-7-28
Thanks, this is correct answer but i want to continue the comparison for the whole matrices by looping sine i am comparing two images, that's why i need this, like for the first column it does, now for the second and third column, then next row, then first and second column, so on.
per isakson
per isakson 2013-7-28
编辑:per isakson 2013-7-28
Something like this might do it?
sza = ...
for ii = 1 : ...
for jj = 1 : ...
is_greater_equal = ( a(1:sza(1),1:sza(2)) >= b(ii:sza(1),jj:sza(2)) )
...
end
end

请先登录,再进行评论。


Image Analyst
Image Analyst 2013-7-28
Please explain what you're trying to do, rather than how you want to solve it. Because I'm thinking that normalized cross correlation (with function normxcorr2) will help you but I'm not really sure what you want to do. And don't just say you want to scan a large matrix with a small one, say WHY you want to do that so we know whether to recommend normxcorr2() or blockproc(), or if you really need nested looping.
  8 个评论
Image Analyst
Image Analyst 2019-7-2
Threshold the correlation output image. If there are no pixels above some threshold, then there are no places in your image where your template can be found.
Ravi Singh
Ravi Singh 2019-7-17
Hello Image Analyst,
Thanks for your kind help. I got the solution for my query..

请先登录,再进行评论。


Azzi Abdelmalek
Azzi Abdelmalek 2013-7-28
B=randi(100,9) % the big matrix
[n,m]=size(B);
S=randi(100,3) % the small matrix
out=[];
for id1=1:3:n-3
for id2=1:3:m-3
a=B(id1:id1+2,id2:id2+2);
out=[out;a(a>=S)];
end
end
  3 个评论
Sonny Sood
Sonny Sood 2013-7-30
512x512 and 256x20, basically it can be any two different sized matrices since i am using two different sized images.

请先登录,再进行评论。


Azzi Abdelmalek
Azzi Abdelmalek 2013-7-30
编辑:Azzi Abdelmalek 2013-7-30
B=randi(100,512,512) ; % the big matrix
[n,m]=size(B);
S=randi(100,256,20) % the small matrix
[n1,m1]=size(S);
q1=fix(n/n1);
q2=fix(m/m1);
idxr= [ n1*ones(1,q1) n-n1*q1];
idxc=[m1*ones(1,q2) m-m1*q2];
idxc(~idxc)=[];
idxr(~idxr)=[];
out=[];
ii0=1;
for id1=1:numel(idxr)
ii1=ii0+idxr(id1)-1;
jj0=1;
for id2=1:numel(idxc)
jj1=jj0+idxc(id2)-1;
a=B(ii0:ii1,jj0:jj1);
[nn,mm]=size(a);
b=S(1:nn,1:mm);
out=[out;a(a>=b)];
jj0=jj1+1;
end
ii0=ii1+1;
end
  2 个评论
Sonny Sood
Sonny Sood 2013-8-5
Can you please add comments for each line, as I am beginner in this, and also will be easy to grab the logic.
Thanks
Image Analyst
Image Analyst 2013-8-5
I had comments in my answer. Don't be intimidated by all the fancy stuff I put in there to make a fancy demo. The basic heart of the code is only two lines: one line to call normxcorr2() and one to call max(). The rest is just comments and well-commented things to make a nice gui.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Geometric Transformation and Image Registration 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by