Comparing all the elements of an array with all the elements of another array

Hello ,
I want to compare Compare all the elements of an array with all the elements of another array and save the result to a now array.
i have an array A=[100 110 120 130.....600]
which i want to compare with B = [100 103 105 120 123 128 130 200 205 207 300 200 100 ]
if the value of B mateches with any element of array the value of A sholud be stored in new array C and the value should not repeal(like if there is already 100 in the array C then i should not write it again. )
the output should look like this
C = [100 120 130 200 300]

 채택된 답변

Bruno Luong
Bruno Luong 2020년 10월 20일
편집: Bruno Luong 2020년 10월 20일
This returns the elements in order as in B, from left to right
A = 100:10:600
B = [100 103 105 120 123 128 130 200 205 207 300 200 100 ]
C = unique(B(ismember(B,A)),'stable')

추가 답변 (1개)

Try this
C = A(ismember(A,B))

댓글 수: 3

CV
CV 2020년 10월 21일
편집: CV 2020년 10월 21일
thanks for the solution.
if i want to set the limit lets say if the value of element of B is less or grater than the element of the A minus 2 or plus 2 then also it should return the element of A.
example of there is 102, 198 and 99 in B then also C should be 100 and 200.
Change statement
ismember(...)
to
ismembertol(..., 2, 'DataScale', 1)
CV
CV 2020년 10월 21일
편집: CV 2020년 10월 21일
i tried the statement of B = [102 150 175 200 205 210 300 99]; an it returns C = [102 150 200 210 300 99] but i should return C [100 150 200 210 300]
but after executing
C = A(ismembertol(A,B,2, 'DataScale', 1))
it returns the correct answer.
thanks

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

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

질문:

CV
2020년 10월 20일

편집:

CV
2020년 10월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by