I have a MATLAB cell array of strings and a second array with partial strings:
base = {'a','b','c','d'}
all2 = {'a1','b1','c1','d1','a2','b2','c2','d2','q8','r15'}
The output is:
base =
'a' 'b' 'c' 'd'
all2 =
'a1' 'b1' 'c1' 'd1' 'a2' 'b2' 'c2' 'd2' 'q8' 'r15'
Problem/Requirement
If any of 'a1','b1','c1','d1' AND any of 'a2','b2','c2','d2' are present in the all2 array, then return a variable numb=2.
If any of 'a1','b1','c1','d1' AND any of 'a2','b2','c2','d2' AND any of 'a3','b3','c3','d3' are present in the all2 array, then return a variable numb=3.
Attempts
1.
Based on strfind(this approach), I tried matches = strfind(all2,base); but I got this error:
`Error using strfind`
`Input strings must have one row.`
....
2.
This other approach using strfind seemed better but just gave me
fun = @(s)~cellfun('isempty',strfind(all2,s));
out = cellfun(fun,base,'UniformOutput',false)
idx = all(horzcat(out{:}));
idx(1,1)
out =
[1x10 logical] [1x10 logical] [1x10 logical] [1x10 logical]
ans =
0
Neither of these attempts have worked. I think my logic is incorrect.
3.
This answer allows to find all indices of an array of partial strings in an array of strings. It returns:
base = regexptranslate('escape', base);
matches = false(size(all2));
for k = 1:numel(all2)
matches(k) = any(~cellfun('isempty', regexp(all2{k}, base)));
end
matches
Output:
matches =
1 1 1 1 1 1 1 1 0 0
My problem with this approach: How do I use the output matches to calculate numb=2? I am not sure if this is the most relevant logic for my specific question since it only gives matching indices.
Question
Is there a way to do this in MATLAB?
EDIT
Additional Information:
The array all2 WILL always be contiguous. A scenario of all2 = {'a1','b1','c1','d1','a3','b3','c3','d3','q8','r15'} is not possible.
all2 = {'a1' 'a3' 'a4'};Should that returnnumb = 3?numbshould be1for that caseall2 = {'a1', 'a3', 'a4'};. If so, then you are correct. If,all2 = {'a1', 'a3' ,'a4'}then the return should benumb=3. Using my example in the OP: If any of'a1',...AND any of'a2',...AND any of'a3',...are present in theall2array, then return a variablenumb=3.'a2, ...'in his example...a2. However, there are stilla3anda4. So both contiguous and non-contiguous are required.