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

12 次查看(过去 30 天)
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

更多回答(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