フィルターのクリア

Find same numbers in two vectors

18 ビュー (過去 30 日間)
Robert Schumann
Robert Schumann 2021 年 6 月 2 日
コメント済み: Johannes Fischer 2021 年 6 月 2 日
Hello,
i need to find same numbers in two vectors and write them down in a third one. Example:
a = [3,8,10,11]
b = [2,3,10,12]
The result should be this vector:
result = [3,10]
Do you have an idea how to do this?
Thanks
Robert

採用された回答

Johannes Fischer
Johannes Fischer 2021 年 6 月 2 日
I think what you need is the ismember function:
a = [3,8,10,11]
b = [2,3,10,12]
result = a(ismember(a, b))
% or
result = b(ismember(b, a))
  2 件のコメント
Robert Schumann
Robert Schumann 2021 年 6 月 2 日
Thank you very much!!
Johannes Fischer
Johannes Fischer 2021 年 6 月 2 日
Keep in mind that the results will be different, when a number occurr multiple times
a = [3,8,10,11,3];
b = [2,3,10,10,12];
resultA = a(ismember(a, b))
resultB = b(ismember(b, a))
leads to
resultA =
3 10 3
resultB =
3 10 10
You can avoid this using unique:
resultA = unique(a(ismember(a, b)))
resultB = unique(b(ismember(b, a)))
will lead to
resultA =
3 10
resultB =
3 10

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeComputer Vision with Simulink についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by