split - Convert matlab symbol to array of products -
can convert symbol product of products array of products?
i tried this:
syms b c d; d = a*b*c; factor(d);
but doesn't factor out (mostly because isn't factor designed do).
ans = a*b*c
i need work if b or c replaced arbitrarily complicated parenthesized function, , nice without knowing variables in function.
for example (all variables symbolic):
d = x*(x-1)*(cos(z) + n); factoring_function(d);
should be: [x, x-1, (cos(z) + n)]
it seems string parsing problem, i'm not confident can convert symbolic variables afterwards (also, string parsing in matlab sounds tedious).
thank you!
use regexp
on string split based on *
:
>> str = 'x*(x-1)*(cos(z) + n)'; >> factors_str = regexp(str, '\*', 'split') factors_str = 'x' '(x-1)' '(cos(z) + n)'
the result factor_str
cell array of strings. convert cell array of sym
objects, use
n = numel(factors_str); factors = cell(1,n); %// each cell hold sym factor n = 1:n factors{n} = sym(factors_str{n}); end
Comments
Post a Comment