Indexing an array with find(contains()) while retaining duplicate elements.

조회 수: 11 (최근 30일)
Joes Edders
Joes Edders 2022년 4월 1일
댓글: Voss 2022년 4월 1일
Hello,
I have two cell arrays containing strings. 'A' contains a list of unique identifying numbers (as strings) and 'B' contains a list of those IDs with the potential for duplicates.
I am attempting to produce an array, the size of 'B', which contains the indices of the relative elements of 'B' within 'A'.
I have previously been using find(contains(X,y)) to extract singular array indices but I am unsure how to expand this to take two arrays while also retaining the duplicate indices, or even if it's appropriate to attempt to use these functions to do so.
I have had success in getting what I want with the code below but would welcome any suggestions on how to do this in-line.
Thanks
A = {'000000699';'000002373';'000002374';'000002378';'000002385';'000002387';'000002389';'000000895';'000001037';'000001038';'000001041';'000001043';'000001045';'000001052';'000001371';'000001746';'000000670';'000000537';'000001193';'000002284';'000002285';'000002286';'000002298';'000002299';'000002300';'000002301';'000002302';'000002315';'000002317';'000002330';'000002468';'000002513';'000000479'};
B = {'000000479';'000000537';'000000670';'000000699';'000000895';'000001037';'000001038';'000001041';'000001043';'000001043';'000001045';'000001052';'000001193';'000001371';'000001371';'000001746';'000002284';'000002285';'000002286';'000002298';'000002299';'000002300';'000002300';'000002301';'000002302';'000002302';'000002315';'000002317';'000002317';'000002330';'000002373';'000002374';'000002374';'000002374';'000002378';'000002385';'000002387';'000002389';'000002468';'000002513';'000002513'};
for i = 1:length(B)
C(i) = find(contains(A,B(i)));
end

채택된 답변

Voss
Voss 2022년 4월 1일
You can use ismember:
A = {'000000699';'000002373';'000002374';'000002378';'000002385';'000002387';'000002389';'000000895';'000001037';'000001038';'000001041';'000001043';'000001045';'000001052';'000001371';'000001746';'000000670';'000000537';'000001193';'000002284';'000002285';'000002286';'000002298';'000002299';'000002300';'000002301';'000002302';'000002315';'000002317';'000002330';'000002468';'000002513';'000000479'};
B = {'000000479';'000000537';'000000670';'000000699';'000000895';'000001037';'000001038';'000001041';'000001043';'000001043';'000001045';'000001052';'000001193';'000001371';'000001371';'000001746';'000002284';'000002285';'000002286';'000002298';'000002299';'000002300';'000002300';'000002301';'000002302';'000002302';'000002315';'000002317';'000002317';'000002330';'000002373';'000002374';'000002374';'000002374';'000002378';'000002385';'000002387';'000002389';'000002468';'000002513';'000002513'};
% using contains()
for i = 1:length(B)
C(i) = find(contains(A,B(i)));
end
C
C = 1×41
33 18 17 1 8 9 10 11 12 12 13 14 19 15 15 16 20 21 22 23 24 25 25 26 27 27 28 29 29 30
% using ismember()
[~,C_new] = ismember(B,A)
C_new = 41×1
33 18 17 1 8 9 10 11 12 12
isequal(C_new.',C)
ans = logical
1
  댓글 수: 2
Joes Edders
Joes Edders 2022년 4월 1일
Thank you. For some reason I had dismissed ismember. Good to know that it returns indices.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by