MATLAB Answers

How to find a vector multiple times in an array?

1 ビュー (過去 30 日間)
Tom Engels
Tom Engels 2021 年 8 月 5 日
コメント済み: Tom Engels 2021 年 8 月 5 日
Good day to all,
I am facing the problem that I need to quickly find the positions of duplicates of a vector in an array.
Currently I am doing this with a for-statement. For this I run the whole array and compare the rows with my vector. The row has the same length as the vector. This leads to very high run times.
With "ismember" I can check if there are duplicates and also determine the position of the first duplicate. However, the vector can occur several times in the array.
Example:
A = [1 3 4] %x,y,z-coordinates - vector
B = [2 3 4; 1 3 4; 5 5 6; 3 4 6; 1 3 4]; %array
The goal here would be to get the indexes 2 and 5.
Can anyone help me with this?
Many greetings
Tom

採用された回答

Chunru
Chunru 2021 年 8 月 5 日
A = [1 3 4] %x,y,z-coordinates - vector
A = 1×3
1 3 4
B = [2 3 4; 1 3 4; 5 5 6; 3 4 6; 1 3 4]; %array
find(all(A==B, 2)) % 2 is for row dim
ans = 2×1
2 5

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2021 年 8 月 5 日
編集済み: Bruno Luong 2021 年 8 月 5 日
A = [1 3 4]; %x,y,z-coordinates - vector
B = [2 3 4; 1 3 4; 5 5 6; 3 4 6; 1 3 4]; %array
% which row match
find(ismember(B,A,'rows'))
ans = 2×1
2 5
  1 件のコメント
Tom Engels
Tom Engels 2021 年 8 月 5 日
Thank you all very much. It works great!

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by