how to find position of an element in a matrix which is present in another matrix?

2 ビュー (過去 30 日間)
ASHA PON
ASHA PON 2022 年 4 月 26 日
コメント済み: ASHA PON 2022 年 4 月 26 日
I am having two matrix. Selected elements in one matrix is present as elements in another matrix. Now i need to find the position of selected elements in the original matrix.
For example:
a=[0.74 0.71 0.604 0.47 0.59 0.58 0.75];
b=[0.604 0.75];
Expected output:
c=[3 7]
I need to apply this problem to a larger n*m matrix.
  2 件のコメント
Stephen23
Stephen23 2022 年 4 月 26 日
Do not use ISMEMBER (unless you really know what you are doing with binary floating point numbers).
Use ISMEMBERTOL instead.
ASHA PON
ASHA PON 2022 年 4 月 26 日
Thank you for your reply.

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

採用された回答

DGM
DGM 2022 年 4 月 26 日
編集済み: DGM 2022 年 4 月 26 日
a=[0.74 0.71 0.604 0.47 0.59 0.58 0.75];
b=[0.604 0.75];
% get the index of matches
% ainb are the indices of elements of a which are in b
% bina is a logical array describing which elements of b are in a
[bina ainb] = ismember(b,a)
bina = 1×2 logical array
1 1
ainb = 1×2
3 7
% get the matched values
a(ainb)
ans = 1×2
0.6040 0.7500
Two things:
First, if you want to get row and column subscripts for use with a 2D array, you should be able to convert idx using sub2ind().
Second, for floating-point inputs, it may be a good idea to use ismembertol() instead of ismember()

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by