Find index to rows of one matrix in another

2 ビュー (過去 30 日間)
Bill Tubbs
Bill Tubbs 2022 年 8 月 31 日
編集済み: Bill Tubbs 2022 年 8 月 31 日
Suppose I have two matrices:
>> A = eye(3)
A =
1 0 0
0 1 0
0 0 1
>> idx = [1 3 2 2];
>> B = A(idx, :)
B =
1 0 0
0 0 1
0 1 0
0 1 0
But I don't know idx. How would I find the vector idx such that isequal(B, A(idx, :))?
I found answers for calculating the index of one row at a time:
>> find(ismember(A, B(2,:), 'rows'))
ans =
3

採用された回答

Bill Tubbs
Bill Tubbs 2022 年 8 月 31 日
編集済み: Bill Tubbs 2022 年 8 月 31 日
I figured out the answer:
>> [~, idx] = ismember(B, A, 'rows')
idx =
1
3
2
2
In case you want to check that all rows in B where found in A you can do this:
[matches, idx] = ismember(B, A, 'rows')
assert(all(matches))

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by