how to find position of an element in a matrix which is present in another matrix?
9 次查看(过去 30 天)
显示 更早的评论
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
2022-4-26
Do not use ISMEMBER (unless you really know what you are doing with binary floating point numbers).
Use ISMEMBERTOL instead.
采纳的回答
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)
% get the matched values
a(ainb)
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 Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!