Hi,
Is there a fast way to compare the differences between two matrices with the same dimension:
example Assume A and B each is a 1000x1000 matrix. Is there a way to find where A and B differ in one step?

답변 (4개)

Jan
Jan 2011년 3월 10일

1 개 추천

And if the matrices are results of floating point computation a certian relative or absolute tolerance might be helpful:
abs_d = (A - B) < Tol
rel_d = ((A - B) ./ min(A, B)) < Tol
And now the same tools as replied by Walter, Sean and Matt can be applied.
Walter Roberson
Walter Roberson 2011년 3월 10일

0 개 추천

find(A~=B)
Sean de Wolski
Sean de Wolski 2011년 3월 10일

0 개 추천

The logical matric
ABne = A~=B
or if you need the indices
[r c] = find(ABne);
Matt Fig
Matt Fig 2011년 3월 10일

0 개 추천

And a visual method:
spy(A~=B)
And a quick method to count the number of locations where A is not equal to B:
nnz(A~=B)

댓글 수: 8

Sean de Wolski
Sean de Wolski 2011년 3월 10일
Matt, I get NNZ taking significantly longer than sum(:):
A = rand(1000)>.5;
B = rand(1000)>.5;
idx = A~=B;
t1 = 0;
t2 = 0;
for ii = 1:10
tic
S1 = sum(idx(:));
t1 = t1+toc;
tic
S2 = nnz(idx);
t2 = t2+toc;
end
isequal(S1,S2)
t1/t2
ans =
1
ans =
0.13468
Jan
Jan 2011년 3월 10일
@Sean: There have been several discussion in CSSM about SUM versus NNZ. The winner changed with the Matlab versions. To my surprise SUM(SUM(idx)) is 45% faster than SUM(idx(:)) even on my single core processor in Matlab 2009a.
Sean de Wolski
Sean de Wolski 2011년 3월 10일
Interesting Jan. I have sum(sum()) is about equal on my MAC laptop running 2009b 64bit.
Matt Fig
Matt Fig 2011년 3월 10일
@Sean, yes, NNZ is slower than SUM - I don't recommend using it in a loop.
Who has a guess as to what NNZ is doing with its time under the hood?
Sean de Wolski
Sean de Wolski 2011년 3월 10일
It was last updated (according to my 2009b) in 2006. Could that have something to do with its sluggishness?
Matt Fig
Matt Fig 2011년 3월 10일
I don't think that is it. I bet TMW knew how to program a simple FOR loop in C back in 2006. I could be wrong of course :).
Jan
Jan 2011년 3월 10일
NNZ is *much* faster in SSE, especially if the data are aligned at 128 bit boundaries.
Sean de Wolski
Sean de Wolski 2011년 3월 10일
I find it funny how the help for nnz says:
The density of a sparse matrix S is nnz(S)/prod(size(S))"
But M-Lint now says:
numel(S) is faster than prod(size(S))"

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

카테고리

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

태그

질문:

2011년 3월 10일

댓글:

2014년 7월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by