Comparing two Matrix for similar elements

조회 수: 3 (최근 30일)
Nikita Johnson
Nikita Johnson 2017년 5월 24일
댓글: Walter Roberson 2022년 1월 6일
suppose you run a check between two matrix, like c=0; if A(1,1) == B (1, 1:3) c=c+1; else c=0; end and the output(ans) is (true, true, false) , I mean (1 1 0).
my counter(c) should run twice(if if A(1,1), B(1,1) and B(1,2) is equal to 8) but it will show c=0. I need C = 2
Other way I found is - Count only when it is true, like
if A(1,1) == B(1,1) c=c+1; else c=0; end
but in the second case, i have to do it for all B elements, and that's not good for big matrix. how to check and count?
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2017년 5월 24일
Please attach your matrices A and B storing in mat - file.
Jan
Jan 2017년 5월 24일
The question is not clear:
c=0;
if A(1,1) == B (1, 1:3)
c=c+1;
else
c=0;
end
Note that A(1,1) == B (1, 1:3) replies a vector, but if requires a scalar condition. Therefore Matlab inserts an all() implicitely.
and the output(ans) is (true, true, false) , I mean (1 1 0).
The shown code does not have any output. It replies either c=1 or c=0, but not [1,1,0].
my counter(c) should run twice(if if A(1,1), B(1,1) and B(1,2)
is equal to 8)
I cannot folow you here.

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

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2017년 5월 24일
편집: Andrei Bobrov 2017년 5월 24일
c = nnz(A(:)' == B(:)); % MATLAB >= R2016b
c = nnz(bsxfun(@eq, A(:)', B(:))); % MATLAB <= R2016a
  댓글 수: 3
Walter Roberson
Walter Roberson 2019년 12월 6일
Only in the sense that some objects cannot be compared. For example if A is object class tf (transfer function) and B is object class primitive chart line, then you would get an error message. But the code would be fine with A being double and B being character for example.
The original question implied that the array sizes are not the same, so the code should not be checking that they are the same.
Daniel Brower
Daniel Brower 2019년 12월 6일
I mean if A is an nxn matrix of real numbers and B is an nxn matrix of real numbers, how would I check if A and B are similar matrices to each other, in the sense that B=S^-1*A*S.

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


Jan
Jan 2017년 5월 24일
편집: Jan 2017년 5월 24일
Does only A(1,1) occur in your porblem? Then:
c = (A(1,1) == B)
or if the number of equal elements is wanted
c = sum(A(1,1) == B) % or nnz() instead of sum()
If all elements of A are concerned, see either Andrei's solution, or:
c = sum(ismember(A(:), B(:)) % Or B(:), A(:) ?
  댓글 수: 2
Zeina Abdullah
Zeina Abdullah 2022년 1월 5일
@Jan can you help me by answer my question in my profile please . i am tring to find similar problem but I did not find
Walter Roberson
Walter Roberson 2022년 1월 6일
https://www.mathworks.com/matlabcentral/answers/1623080-how-to-find-relationship-between-two-or-more-matrix?s_tid=srchtitle appears to be the reference

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

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by