matlab - 在Matlab中将符号分解的输出转换为矩阵表示

标签 matlab symbolic-math

输出


因子(sym('12345678901234567890'))



ans =

2*3^2*5*101*3541*3607*3803*27961

是否有一种简单的方法可以将输出更改为:
[2 3 5 101 3541 3607 3803 27961; 1 2 1 1 1 1 1 1]?

最佳答案

试试这个代码:

aux = char(factor(sym('12345678901234567890'))); % convert to string
aux = regexprep(aux, {'*'}, ''',''');            % replace '*' with '',''
result = eval( ['{''',aux,'''}']);               % evaluate string expression

% Separate each string into tokens around the '\^' character
factor = cell(1,length(result));
exponent = cell(1,length(result));
for i=1:length(result)
    factor{i} = regexpi(result{i}, '(\w*)\^', 'tokens');
    if isempty(factor{i})
        factor{i} = result{i};
    else
        factor{i} = char(factor{i}{1});
    end
    exponent{i} = regexpi(result{i}, '\^(\w*)', 'tokens');
    if isempty(exponent{i})
        exponent{i} = '1';
    else
        exponent{i} = char(exponent{i}{1});
    end
end

% Converting the cellstr to normal matrices
factors = zeros(length(factor),1);
exponents = zeros(length(exponent),1);
for i=1:length(factor)
    factors(i) = str2num(factor{i});
    exponents(i) = str2num(exponent{i});
end

clear result factor exponent i aux;

实际上,解决方案类似于Kokbira的建议。

希望能帮助到你,

关于matlab - 在Matlab中将符号分解的输出转换为矩阵表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10067880/

相关文章:

matlab - 使用 MATLAB 获取信号的包络

matlab - 如何在 MATLAB 中绘制不同角度的图形

matrix - sympy:收集矩阵系数的符号?

python - Sympy 替代数学表达式

matlab - MATLAB 教程中的 SIFT 实现

matlab - 为什么神经网络的准确性不好?

sympy - Pharo 有 CAS 吗?

string - 将单词转换为唯一标识符

matlab - 为 syms 创建多个变量

javascript - 在 Java 中将用户输入评估为 JavaScript 时的安全风险