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!