フィルターのクリア

Using find to determine equality not of scalars, but vectors

2 ビュー (過去 30 日間)
Josue Ortega
Josue Ortega 2023 年 5 月 26 日
コメント済み: Stephen23 2023 年 5 月 26 日
Hi, I have two vectors say x=[1 2 3 4 5 ] and y=[2 4].
I would like to find the elements of y in x. Usually, if y is a scalar, a simple find(x==2) would do teh trick, or even x(x==2).
Is there something similar that would work like find(x==y) or x(x==y), without using a loop?

回答 (2 件)

John D'Errico
John D'Errico 2023 年 5 月 26 日
編集済み: John D'Errico 2023 年 5 月 26 日
This is not really the province of find. Yes, you can break an egg with a hammer, but it is not really the right tool for the purpose. (Ok, yes, the scrambled eggs made that way will be high in fiber. But they tend to be a little too crunchy for my tastes.) Instead, you need to learn about the set membership tools in MATLAB.
First, I'll make the problem slightly more interesting, since using the numbers 1:5 may confuse things.
x=[1:3:21]
x = 1×7
1 4 7 10 13 16 19
y=[2:2:20]
y = 1×10
2 4 6 8 10 12 14 16 18 20
There are clearly some elements in y that are not in x, and vice versa.
help ismember
ISMEMBER True for set member. LIA = ISMEMBER(A,B) for arrays A and B returns an array of the same size as A containing true where the elements of A are in B and false otherwise. LIA = ISMEMBER(A,B,'rows') for matrices A and B with the same number of columns, returns a vector containing true where the rows of A are also rows of B and false otherwise. [LIA,LOCB] = ISMEMBER(A,B) also returns an array LOCB containing the lowest absolute index in B for each element in A which is a member of B and 0 if there is no such index. [LIA,LOCB] = ISMEMBER(A,B,'rows') also returns a vector LOCB containing the lowest absolute index in B for each row in A which is a member of B and 0 if there is no such index. The behavior of ISMEMBER has changed. This includes: - occurrence of indices in LOCB switched from highest to lowest - tighter restrictions on combinations of classes If this change in behavior has adversely affected your code, you may preserve the previous behavior with: [LIA,LOCB] = ISMEMBER(A,B,'legacy') [LIA,LOCB] = ISMEMBER(A,B,'rows','legacy') Examples: a = [9 9 8 8 7 7 7 6 6 6 5 5 4 4 2 1 1 1] b = [1 1 1 3 3 3 3 3 4 4 4 4 4 9 9 9] [lia1,locb1] = ismember(a,b) % returns lia1 = [1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1] locb1 = [14 14 0 0 0 0 0 0 0 0 0 0 9 9 0 1 1 1] [lia,locb] = ismember([1 NaN 2 3],[3 4 NaN 1]) % NaNs compare as not equal, so this returns lia = [1 0 0 1], locb = [4 0 0 1] Class support for inputs A and B, where A and B must be of the same class unless stated otherwise: - logical, char, all numeric classes (may combine with double arrays) - cell arrays of strings (may combine with char arrays) -- 'rows' option is not supported for cell arrays - objects with methods SORT (SORTROWS for the 'rows' option), EQ and NE -- including heterogeneous arrays derived from the same root class See also ISMEMBERTOL, INTERSECT, UNION, UNIQUE, UNIQUETOL, SETDIFF, SETXOR, SORT, SORTROWS. Documentation for ismember doc ismember Other uses of ismember categorical/ismember datetime/ismember sym/ismember cell/ismember duration/ismember tabular/ismember codistributed/ismember gpuArray/ismember tall/ismember dataset/ismember mtree/ismember
[yinx,xloc] = ismember(y,x)
yinx = 1×10 logical array
0 1 0 0 1 0 0 1 0 0
xloc = 1×10
0 2 0 0 4 0 0 6 0 0
So which elements of y were found in x?
y(yinx)
ans = 1×3
4 10 16
That makes sense. Next, where were they found?
xloc(yinx)
ans = 1×3
2 4 6
So at locations [2,4,6] in the x vector.
You will also find the setdiff tool to be useful at times, as well as intersect and union. Spend some time familiarizing yourself with many of these fundamental tools in MATLAB. It will be well worth the time invested.
  1 件のコメント
Stephen23
Stephen23 2023 年 5 月 26 日
And here are all of the basic set functions:
Note the contents on the LHS of the page: learn to navigate this so that you can find information by yourself.

サインインしてコメントする。


Harsh Mahalwar
Harsh Mahalwar 2023 年 5 月 26 日
編集済み: Harsh Mahalwar 2023 年 5 月 26 日
Hi Josue,
You can use the following matlab script to find the elements of y in x without using a loop.
y = [2 4];
x = [1 2 3 4 5];
findLoc = arrayfun(@(i) find(x==i,1),y);
Here are some references,
Thanks and regards,
Harsh Mahalwar

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by