Finding elements in string or cell array with common strings

Consider two arrays A & B:
A={'blah_12_blah' 'blah_456_blah' 'blah_789_blah' 'blah_NPQZ_blah'}
B={'blah_NPQZ_blah' 'blah_135_blah' 'blah_579_blah' 'blah_12_blah' 'blah_RSTX_blah'}
The termn 'blah' basically refers to any string that comes before and after the parts of interest and each 'blah' could be a different string.
I would like to compare these two arrays and fine only the members that have the "part of interest" in common. In this case it means that after comparing A & B, only the elements 'blah_12_blah' and 'blah_NPQZ_blah' will show up as output, since these elements have the parts '12' and 'NPQZ' incommon, no matter what their respective 'blah' parts are.

 Akzeptierte Antwort

A={'blah_12_blah' 'blah_456_blah' 'blah_789_blah' 'blah_NPQZ_blah'}
A = 1×4 cell array
{'blah_12_blah'} {'blah_456_blah'} {'blah_789_blah'} {'blah_NPQZ_blah'}
B={'blah_NPQZ_blah' 'blah_135_blah' 'blah_579_blah' 'blah_12_blah' 'blah_RSTX_blah'}
B = 1×5 cell array
{'blah_NPQZ_blah'} {'blah_135_blah'} {'blah_579_blah'} {'blah_12_blah'} {'blah_RSTX_blah'}
A_partofinterest = extractBetween(A,'_','_')
A_partofinterest = 1×4 cell array
{'12'} {'456'} {'789'} {'NPQZ'}
B_partofinterest = extractBetween(B,'_','_')
B_partofinterest = 1×5 cell array
{'NPQZ'} {'135'} {'579'} {'12'} {'RSTX'}
A_new = A(ismember(A_partofinterest,B_partofinterest))
A_new = 1×2 cell array
{'blah_12_blah'} {'blah_NPQZ_blah'}
B_new = B(ismember(B_partofinterest,A_partofinterest))
B_new = 1×2 cell array
{'blah_NPQZ_blah'} {'blah_12_blah'}

5 Kommentare

Hi Kevin, this is really good, and I didn't know that this command existed.
However, it looks like I misrepresented the question by adding the '_' character implying that it is a fixed part of the member. Actually, it is not, and I just put it there to separate the blahs from the parts of interest for better visibility. In other words, the elements would look more like this:
'blah123blah'
'blah456blah'
etc.
That makes it a bit more challenging. Here is my solution.
A={'blah12balh' '54324569689' 'asdfkj789oiups' '+_()_NPQZ@$#%'}
A = 1×4 cell array
{'blah12balh'} {'54324569689'} {'asdfkj789oiups'} {'+_()_NPQZ@$#%'}
B={'wwelnkNPQZcvou' 'utcim135xzus' 'rsnfu579sodm' '9f6ej12d9j37' '0o1q9i2wRSTX7y3e64tr'}
B = 1×5 cell array
{'wwelnkNPQZcvou'} {'utcim135xzus'} {'rsnfu579sodm'} {'9f6ej12d9j37'} {'0o1q9i2wRSTX7y3e64tr'}
Assuming two characters have to be the same.
A_new_index = [];
B_new_index = [];
for b = 1:length(B)
for a = 1:length(A)
for ii = 1:length(A{a})-1
if contains(B{b},A{a}(ii:ii+1)) == 1
display(['part of interest is ' A{a}(ii:ii+1)])
display(['A cell is ' A{a}])
A_new_index = [A_new_index a];
display(['B cell is ' B{b}])
B_new_index = [B_new_index b];
end
end
end
end
part of interest is NP
A cell is +_()_NPQZ@$#%
B cell is wwelnkNPQZcvou
part of interest is PQ
A cell is +_()_NPQZ@$#%
B cell is wwelnkNPQZcvou
part of interest is QZ
A cell is +_()_NPQZ@$#%
B cell is wwelnkNPQZcvou
part of interest is 12
A cell is blah12balh
B cell is 9f6ej12d9j37
A_new = A(1,unique(A_new_index))
A_new = 1×2 cell array
{'blah12balh'} {'+_()_NPQZ@$#%'}
B_new = B(1,unique(B_new_index))
B_new = 1×2 cell array
{'wwelnkNPQZcvou'} {'9f6ej12d9j37'}
Saeid
Saeid am 27 Apr. 2022
Bearbeitet: Saeid am 27 Apr. 2022
Thanks Kevin, I guess this will work, but just one final question about a problem that is somewhere between the first and the second one: what if the reference array (A) has fixed elements (no blahs) and the second one is as before?
A=[123abc 456klm 1a2b3c ...]
B=[blah123abcblah blah789ijkblah blah11aa22bbblah 123abc 456pqr 1a2b3c ...]
In this case I want to find any element of B that has exactly 123abc somewhere in it and so on.
A={'123abc' '456klm' '1a2b3c'}
A = 1×3 cell array
{'123abc'} {'456klm'} {'1a2b3c'}
B={'blah123abcblah' 'blah789ijkblah' 'blah11aa22bbblah' '123abc' '456pqr' '1a2b3c'}
B = 1×6 cell array
{'blah123abcblah'} {'blah789ijkblah'} {'blah11aa22bbblah'} {'123abc'} {'456pqr'} {'1a2b3c'}
contains(B,A)
ans = 1×6 logical array
1 0 0 1 0 1
B(contains(B,A))
ans = 1×3 cell array
{'blah123abcblah'} {'123abc'} {'1a2b3c'}
Cool, thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2022a

Gefragt:

am 25 Apr. 2022

Kommentiert:

am 27 Apr. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by