【问题标题】:Matlab - Put elements of structure into an arrayMatlab - 将结构元素放入数组中
【发布时间】:2017-06-24 15:02:50
【问题描述】:
syms x t 
n=3; a=-1; b=1;
f=@(x) x;
k=@(x,t) x*t;
A=sym('a',[1 n]);
y(x)=A(1);
for i=1:n-1
    y(x)=y(x)+A(1,i+1)*(x^i);
end
I(A)=int((y(x)-f-int(k*y(t),t,a,b))^2,x,a,b)
for i=1:n
    S(i)=diff(I,A(1,i));
end
p=solve(S,A);

我想使用所有元素“p”作为多项式系数。 如何将所有元素放入一个数组中?

【问题讨论】:

    标签: arrays matlab vector structure


    【解决方案1】:

    有两种方式:

    选项1:指定输出参数的数量

    [p1,p2,p3] = solve(S, A);
    p1 = double(p1);
    p2 = double(p2);
    p3 = double(p3);
    

    选项 2:使用 structfun 将输出转换为数组

    p = solve(S,A);
    p = structfun(@double, p);
    

    结果:

    p =
         0
         3
         0
    

    【讨论】:

    • 还有第三种方式。 "struct2array"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多