How to compare two matrix and then count each elements?

The elements of A will be checked and compared with B, with each element count S will increase and with each absence count R will increase.
A=[3 5 7]
B=[1 3 4 5 7]

댓글 수: 5

Can you please clarify what S and R are, and provide an expected output?
@suchismita: Please give more details, so that we can actually understand what you want.
What does "with each element count S will increase " mean: do you want to count how many elements A has, or how many elements B has, or how many elements of A as in B, or how many elements of B are in A?
What does "with each absence count R will increase" mean: do you mean elements of A missing from B, or elements of B missing from A, or missing from their own (somehow implied) sequences?
sorry for late reply ... I want to count how many similar elements are there in A in compare to B. With each count S will increase and with each no R will.
"I want to count how many similar elements are there in A in compare to B"
That is what my answer does. You might also like to read about intersect and setdiff.
S = nnz(ismember(A,B)) this is working but if I am changing value of A as A= [2 5 7] then I want my answer as S=2 and R=1

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

 채택된 답변

Stephen23
Stephen23 2018년 6월 6일
Some variety of these might do what you want (although those examples are not very enlightening)
>> S = nnz(ismember(A,B))
S = 3
>> R = nnz(ismember(B,A))
R = 3

댓글 수: 2

please help me. S value is OK but how can I count dissimilar value.
Stephen23
Stephen23 2018년 6월 10일
편집: Stephen23 2018년 6월 10일
"but how can I count dissimilar value."
ismember returns a logical array. Negate that array to get the dissimilar values:
~ismember(...)

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

추가 답변 (1개)

Pratik Panchal
Pratik Panchal 2018년 6월 6일
It is not clear whether S and R are increasing if the values of A are same as values of B or regardless. Also what do you mean by 'absence'?
Anyway for the comparison You can use a for loop in this.
for k=1:length(A)
for j=1:length(B)
if A(k)==B(j);
%Insert your condition here
else
%Insert your second condition here
end
end

카테고리

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

질문:

2018년 6월 6일

편집:

2018년 6월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by