How to find sub-strings in an array of strings, extract and store them (using "contains" function) without "loops" and "for"
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a sample(A) and reference lists (B) and i want to find a substring, extract and store as showed in the example below:
Sample A is a 3X1 string array
A = ["roots of Datura stramonium";"roots of Datura stramonium, Datura first";"roots of Datura stramonium, Datura first, Datura second"]
Reference B is a 3X1 string array
B = ["Datura first";"Datura second","Datura Stramonium"]
I want to find each element of B in A and storage the results like in example Result:
Result= ["roots of Datura stramonium","Datura stramonium",missing,missing;
"roots of Datura stramonium, Datura first","Datura stramonium","Datura first",missing;
"roots of Datura stramonium, Datura first, Datura second","Datura stramonium","Datura first","Datura second"]
For that I was trying to use find and contains functions as follows:
Spec_cont=find(contains(A(1:end,1),B, 'IgnoreCase',true));
However, constains doesn't support multiple entries at once. So I used a loop to access each element of B. Since I have almost 100k rows this takes a long time to perform. How can I avoid loops in this case?
Second question:
How can I store Spec_cont outputs as shown in the Result output, since I will have different row sizes (for each loop i have to store in a different place)
Currently My code is like that:
for j=1:length(B)
Spec_cont=find(contains(A(1:end,1),B, 'IgnoreCase',true));
if isempty(Spec_cont)==1
else
for k=1:length(Spec_cont)
Position=size(A,2)-sum(ismissing(A(Spec_cont(k,1),1:end)));
A(Spec_cont(k,1),Position+1)=B;
clear Position
end
end
end
Thank you very much
Alan
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!