Indexing an array with find(contains()) while retaining duplicate elements.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
Voss
am 1 Apr. 2022
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
% using ismember()
[~,C_new] = ismember(B,A)
isequal(C_new.',C)
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!