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!
