Get the index from the comparison of two different lenght vectors

9 ビュー (過去 30 日間)
Giovanni
Giovanni 2011 年 9 月 14 日
Hi,
I would like to compare two vectors, and get the indexes of the positions where the two vectors are matching. In such a way I could create a sort of lookup table with the correspondences between the vectors indexes. For instance, if I have:
A = [1 2 3 5 6 8 9 7 10] B = [1 5 7 9]
indexes = compare(B,A);
I would like to have as a result: indexes = [1 4 9 8] I would have this way a sort of lookup table keeping track of the correspondences between the two vectors.
If I would then obtain the second element of B then I could just say that is A(4) = 5....or.... B(2) = 4;
how could I do this without looping over the whole vector? (which in my case is very huge....)
  1 件のコメント
Giovanni
Giovanni 2011 年 9 月 14 日
Errata corridge: I meant A(4) == B(2) = 5, and B(2) == A(4) = 5

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

採用された回答

Desiree
Desiree 2011 年 9 月 14 日
You can use function INTERSECT for this:
>> A = [1 2 3 5 6 8 9 7 10] ;B = [1 5 7 9];
>> [c, ia, ib] = intersect(A,B)
c =
1 5 7 9
ia =
1 4 8 7
ib =
1 2 3 4
  1 件のコメント
Giovanni
Giovanni 2011 年 9 月 14 日
Greattttt! it´s exactly what I was looking for :)
Thanks!

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

その他の回答 (0 件)

カテゴリ

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

Translated by