How to find the indeces of same values within two vectors with repetitive values?

90 ビュー (過去 30 日間)
Hi all,
I've got two vectors:
A=[1000 2000 2000 5000 20000 20000];
B=[1000 2000 2000 5000 10000 20000 20000 60000 60000 60000 60000 0 0 0 0 0 0 0 0 0 0 0 0 0];
and I want to find the indeces of the elements of B that appears also in A.
I've tried this:
[~,idx]=ismember(A,B);
and it gives me idx=[1 2 2 4 6 6] but I want idx to be [1 2 3 4 6 7].
The problem is given by the repetitive values but I don't know how to fix it. Any ideas? Thanks in advance.

採用された回答

Stephen23
Stephen23 2020 年 5 月 13 日
編集済み: Stephen23 2020 年 5 月 13 日
The example data from your question:
>> A = [1000,2000,2000,5000,20000,20000];
>> B = [1000,2000,2000,5000,10000,20000,20000,60000,60000,60000,60000,0,0,0,0,0,0,0,0,0,0,0,0,0];
>> X = A(:)==B; % requires MATLAB >=R2016b, for earlier versions replace == with BSXFUN.
>> [~,Y] = find(X & cumsum(X,2)==cumsum(X,1))
Y =
1
2
3
4
6
7
The example data from your comment:
>> A = [500,500,5000];
>> B = [250,500,500,1250,2500,5000,5000,15000,15000,15000,15000,8166,8926,9796,10800,11967,13333,14948,16875,16875,19200,22041,22041,25562];
>> X = A(:)==B; % requires MATLAB >=R2016b, for earlier versions replace == with BSXFUN.
>> [~,Y] = find(X & cumsum(X,2)==cumsum(X,1))
Y =
2
3
6
  2 件のコメント
Valentina Mazzoni
Valentina Mazzoni 2020 年 5 月 13 日
Thank you Stephen, it's the second time that you save me!! It works perfectly!!
Akihumi
Akihumi 2020 年 5 月 13 日
Wow that's a brilliant way to do it Stephen! Amazing!

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

その他の回答 (2 件)

Mario Malic
Mario Malic 2020 年 5 月 13 日
Values=ismember(B,A) % Will return true/false for each value of B found in A
Ind_Values = Values .* [1:1:length(B)] % Multiply it by a vector to get actual indices from B
Ind_Values = nonzeros(Ind_Values)' % Remove zeros
Certainly, there is more elegant way to do this.
  3 件のコメント
Mario Malic
Mario Malic 2020 年 5 月 13 日
編集済み: Mario Malic 2020 年 5 月 13 日
Akihumi's answer is great. Maybe you can add if condition to his code to remove extra numbers?
Another suggestion is a function intersect, but it requires that matrix A does not have the same values, otherwise it will not work as you want.
[C,ia,ib] = intersect(A,B) %ib contains indices in B
Valentina Mazzoni
Valentina Mazzoni 2020 年 5 月 13 日
Thank you very much Mario, I've already tried with intersect as you suggest but it didn't work as I wanted. Stephen Cobeldick's code is what I need. Thank you for your time!

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


Akihumi
Akihumi 2020 年 5 月 13 日
編集済み: Akihumi 2020 年 5 月 13 日
flip it to another way should work:
idx = find(ismember(B,A)==1);
  1 件のコメント
Valentina Mazzoni
Valentina Mazzoni 2020 年 5 月 13 日
Thank you Akihumi, your code works perfectly too with the vectors in the question but if I change them, I obtain the same result that I have with Mario's code. Of course if you have any ideas, I'll be very happy to read them.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by