필터 지우기
필터 지우기

Can I find set of values in larger set in one step?

조회 수: 7 (최근 30일)
huda nawaf
huda nawaf 2022년 8월 14일
답변: Ishan Ghosekar 2022년 8월 26일
Hi,
I am working on a code that takes a very long time to implement, so I try to optimize it
If I look for a set of values (x) in a larger set (z), can I do that in a way faster than using for and find?
x=[1 2 3];
z=[10 2 1 5 7 .....];
thanks
  댓글 수: 10
huda nawaf
huda nawaf 2022년 8월 15일
what if I have c ,when I find 1 and 2 in z , I want to remove it from c
x=[1 2 3];
>> z=[10 2 1 5 7];
>> c=[9 4 2 1];
I need the output is
c=[9 4]
Bruno Luong
Bruno Luong 2022년 8월 15일
x=[1 2 3];
z=[10 2 1 5 7];
c=[9 4 2 1];
c(ismember(c,intersect(z,x)))=[]
c = 1×2
9 4

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

채택된 답변

Ishan Ghosekar
Ishan Ghosekar 2022년 8월 26일
As per my understanding, you want to look for the set of values in x in another larger set, z in an optimized way.
The following functions can help you achieve this:
  • The intersect function returns common values between x and z in sorted order:
>> x=[1 2 3];
>> z=[10 2 1 5 7 4 6 9];
>> intersect(x,z)
ans =
1 2
For more information regarding the intersect function, refer to the following documentation link:
  • The ismember function returns an array containing logical 1 (true) where the data in A is found in B.
>> ismember(x,z)
ans =
1×3 logical array
1 1 0
For more information regarding the ismember function, refer to the following documentation link:

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by