作者sunev (Veritas)
看板MATLAB
标题Re: [讨论] 多输入单输出 曲线拟合
时间Tue Oct 30 22:26:01 2018
※ 引述《jimmyjean (色仙)》之铭言:
: 标题: [讨论] 多输入单输出 曲线拟合
: 时间: Thu Oct 25 22:46:47 2018
: 大家好
: 小弟的问题如下
: 假设我有3个独立的输入 想拟合一条2次的曲线 以W=A\y计算 其中A矩阵每一横列都是
: [1 x1 x2 x3 x1*x2 x2*x3 x1*x3 x1^2 x2^2 x3^2]
: 请问有没有函式是可以建立A矩阵的
: 目前的解法是手动更改 但希望可以做到动态追踪输入数及阶数後建立A矩阵
: 请各位大神帮忙了
: 感恩
: --
:
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.137.48.187
: ※ 文章网址: https://webptt.com/cn.aspx?n=bbs/MATLAB/M.1540478809.A.128.html
: 推 rockman037: 做一个ones矩阵 10/29 11:46
: → rockman037: 然後矩阵相乘 10/29 11:48
: 推 sunev: 要一般n次拟合的矩阵比较麻烦,但还是可以用eval加ndgrid 10/30 13:26
: → sunev: 实务上可能针对3次,2次分别处理即可 10/30 13:27
: 推 profyang: http://bit.ly/matlab_file_exchange_polyfitn 这个? 10/30 17:57
: → profyang: 嘛...不过他应该也是用for loop一项项去.*之类的吧 10/30 17:58
: 推 profyang: 看有没有人能想出不用for loop的方法 暂时是想不出来 10/30 18:04
n=3; % number of indepdent variables
m=2; % degree of the fitting polynomial
N=15; % number of data
X=rand(N,n); % testing data
% construct the power of independent variable
x=cell(1,n);
[x{:}]=ndgrid(0:m); % 这一行本来以为要用eval作,後来发现可以用cell
x=cell2mat(cellfun(@(b)b(:),x,'UniformOutput',false));
x(sum(x,2)>m,:)=[];
% some sorting
x=sortrows([x max(x,[],2) sum(x,2)],n+2:-1:1);
x=x(:,1:n);
A=squeeze(prod(X.^reshape(x',[1 n size(x,1)]),2));
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.112.54.158
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/MATLAB/M.1540909565.A.50E.html