Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab中符号分解输出到矩阵表示的转换_Matlab_Symbolic Math - Fatal编程技术网

Matlab中符号分解输出到矩阵表示的转换

Matlab中符号分解输出到矩阵表示的转换,matlab,symbolic-math,Matlab,Symbolic Math,产量 系数(sym('123456789001234567890')) 是 是否有一种简单的方法可以将输出更改为: [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 '*'

产量

系数(sym('123456789001234567890'))

是否有一种简单的方法可以将输出更改为:

[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;
实际上,解决方案与科比拉的建议类似

希望有帮助

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;