how to compare two values that are repeated in two vectors?

조회 수: 5 (최근 30일)
Hello everyone, I'm new to matlab, I wanted to know how I can compare 2 values that are repeated between two vectors, I'll explain myself better, for example I have these two vectors.
A = [10 56 34 11 34 ];
B = [10 56 34 11 ];
Now I need to compare element by element, I mean...
vector B = [(10) 56 34 11]; element 10 is repeated in vector A? No then it is assigned the number 1.
vector B = [10 (56) 34 11 ]; element 56 is repeated in vector A? No then it is assigned the number 1.
vector B = [10 56 (34) 11 ]; element 34 is repeated in vector A? Yes! then it is assigned the number 0.
vector B = [10 56 34 (11) ]; element 34 is repeated in vector A? Yes! then it is assigned the number 1.
the result has to be an array, equal to this.
result= [10 1
56 1
34 0
11 1]
any help will be very helpful.
  댓글 수: 2
Image Analyst
Image Analyst 2022년 6월 2일
편집: Image Analyst 2022년 6월 2일
What if the numbers are not in the same index in the two vectors, like
A = [10 56 34 11 34 ];
B = [11 10 56 34 5 ];
are the 10, 56, and 34 repeated in B? What's your definition of repeated?
And is this your homework (sounds like it)?
giancarlo maldonado cardenas
giancarlo maldonado cardenas 2022년 6월 2일
Maybe I expressed myself in the wrong way!
I meant for example in the vector B = [10 56 (34) 11 ]; in vector A element 34 is repeated twice? Yes! then it is assigned the number 0.
then all the other values of B are not repeated twice in A so it is assigned the label 1, the only value of B repeated twice in A is the value 34, therefore it is assigned the label 0.

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

채택된 답변

Voss
Voss 2022년 6월 2일
편집: Voss 2022년 6월 2일
A = [10 56 34 11 34 ];
B = [10 56 34 11 ];
result = [B.' sum(A==B.',2)<=1]
result = 4×2
10 1 56 1 34 0 11 1
Breaking that expression down:
A==B.' % compare each element of A to every element of B
ans = 4×5 logical array
1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0
sum(A==B.',2) % sum each row to get the number of occurrences in A of each element of B
ans = 4×1
1 1 2 1
sum(A==B.',2)<=1 % if the number of occurrences is NOT 2 or more, that's a 1 in the result, otherwise a 0
ans = 4×1 logical array
1 1 0 1
  댓글 수: 2
giancarlo maldonado cardenas
giancarlo maldonado cardenas 2022년 6월 5일
편집: giancarlo maldonado cardenas 2022년 6월 6일
is there a way to do it with a for loop, and not altogether?
I have to compare each element of vector B and traverse vector A in search of if that element is repeated twice, then an IF condition, if label 0 is repeated, if label 1 is not repeated, but this label can also be 0 with a probability of 10%. this is my code when comparing repeated elements in a single vector.
can u help me?
Voss
Voss 2022년 6월 5일
편집: Voss 2022년 6월 5일
I don't understand what you mean by "if label 0 is repeated, if label 1 is not repeated, but this label can also be 0 with a probability of 10%", but here is how you can perform the task from the original question using a for loop:
A = [10 56 34 11 34 ];
B = [10 56 34 11 ];
result = [B.' zeros(numel(B),1)];
for ii = 1:numel(B)
result(ii,2) = nnz(A==B(ii))<=1;
end
disp(result);
10 1 56 1 34 0 11 1
Maybe that can be of use in your actual task.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 LTE Toolbox에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by