How to identify if a string has a pattern ?
古いコメントを表示
Hi,
I have a set of strings and I want to categorize them into the different set of functions.
Let's consider the following set of strings:
setStrings{1} = '2*x + 3';
setStrings{2} = '2*exp(-3/x)'
setStrings{3} = 'sin(x)'
and the following set of functions:
func1 = @(x) A*x+B
func2 = @(x) A*exp(-B/x)
How we can check that setStrings{1} and setStrings{2} have, respectively, the form of func1 and func2? And how to find out that setStrings{3} does not follow the form of availabla functions?
And finally, is the a way to simplify a string that has several multiplications inside? for instance:
'10*exp(-3/x)*1/5' -> '2*exp(-3/x)'
Thanks in advance for your help.
採用された回答
その他の回答 (1 件)
And finally, is the a way to simplify a string that has several multiplications inside?
If you have the Symbolic Math Toolbox
syms x
simplify(10*exp(-3/x)*1/5)
How we can check that setStrings{1} and setStrings{2} have, respectively, the form of func1 and func2? And how to find out that setStrings{3} does not follow the form of availabla functions?
You could do it by curve fitting each of the setStrings to each of your models and assessing the fitting error. Depending on the variety possible in setStrings you might be able to do things like the following,
setStrings{1} = '2*x+3';
setStrings{2} = '2*exp(-3/x)';
setStrings{3} = 'sin(x)';
N=numel(setStrings);
spl0={'A','B',''};
for i=1:N
spl=split(setStrings{i},digitsPattern)';
if numel(spl)~=3,
setStrings{i}='Unclassified';
else
setStrings{i} = strjoin([spl;spl0],'');
end
end
setStrings
[tf,loc]=ismember(setStrings, {'A*x+B';'A*exp(-B/x)'})
カテゴリ
ヘルプ センター および File Exchange で Data Type Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
