Larger size matrix comparison faster elementwise

I want to find the equal elements in two matrices, but size is larger so for loop taking too much time. please suggest how to increase speed.

댓글 수: 5

It's unclear why you'd need a loop in the first place.
What is your definition of equal elements in two matrices? Same value at the same position? Intersection of the sets formed by each matrix? something else?
thanks for response. I am having two 36*80000 and 36*79820 sizes matrices. i want to find percentage of similarity between them. so if element by element comparison is require. please suggest if any alternate which gives fast response.
Jan
Jan 2017년 7월 4일
편집: Jan 2017년 7월 4일
@ashwin sharma: Please explain what "similarity" means mathematically. What is the wanted output for the mentioned inputs? Please give the readers a chance to post an answer without guessing what "percentage of similarity" means. Is it the number of equal rows divided by the size of the larger array, or sum of sizes of both arrays, or the number of equal elements divided by the number of all elements of both matrices, or what?
number of equal elements divided by the number of all elements
But how do you compare them when they are not the same size? There are ways but you need to tell us. Or do you just want to compare the pairs that are the same size with each other, and not to the different sized ones, like
percentSimilar = sum(sum(m1 == m2)) / numel(m1);

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

답변 (3개)

Guillaume
Guillaume 2017년 7월 4일
Elementwise comparison has never needed a loop:
A == B
is all that is needed. And if you want the values at the position where they're equal:
A(A == B)
Unless you're talking about another sort of equality (e.g. intersect, which also wouldn't need a loop)

댓글 수: 5

Jan
Jan 2017년 7월 4일
편집: Jan 2017년 7월 4일
@Guillaume: Congratulations! It would be nice if you wait a little bit with further answers in the next hours. Then your 10'000 points will occur together with Image Analystss 40'000 and Walter's 60'000. :-) What a decimal party! See https://www.mathworks.com/matlabcentral/answers/contributors/?s_tid=gn_mlc_ans_cnt
Oooh! Well, I've reached 10,000. Better not answer any more questions!
Jan
Jan 2017년 7월 5일
;-) Now Image Analyst reached the 40'000 also - congratulations! But Walter needs 58 points. This can take an hour. Perhaps I should reduce the pressure on your shoulders and vote for one of your excellent answers.
Thanks Jan!
Jan
Jan 2017년 7월 5일
@Guillaume: The magic moment of trailing zeros has gone. Walter has been a little bit too slow - I did not think I'd ever say that. Now please restart to answer questions ;-)

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

Image Analyst
Image Analyst 2017년 7월 4일

1 개 추천

You only need to shift the 36*79820 matrix 180 by 180 within the larger 36*80000 matrix to check matches or other things at each shift location. That's only 14,400 loop iterations - very small and fast. You can compare in a variety of ways, such as psnr(), isequal(), ssim(), normxcorr2(), etc.
Guillaume
Guillaume 2017년 7월 5일
Still not entirely sure what you call similar, particularly with matrices of different size. If you want the number of elements that are common between the two matrices regardless of location, then as said you can use intersect:
numcommonelements = numel(intersect(matrix1(:), matrix2(:)))
However the above will ignore duplicated elements. If you have duplicated elements, two calls to ismember would do:
numcommonin1 = sum(ismember(matrix1(:), matrix2(:)));
numcommonin2 = sum(ismember(matrix2(:), matrix1(:)));

댓글 수: 1

Or perhaps:
nCommonCols = numel(intersect(matrix1.', matrix2.', 'rows'))

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2017년 7월 4일

댓글:

Jan
2017년 7월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by