How to compare two matrix and then count each elements?

3 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2018-6-8
"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.
suchismita
suchismita 2018-6-8
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 个评论
suchismita
suchismita 2018-6-9
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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by