我有以下代码:
a=zeros(1,3);
syms x
y=zeros(1,3);
for j = 1:3
a = [zeros(1,j-1) 1 zeros(1,3-j)];
y(1,j)=x*a(1,j);
display(y(1,j));
end;
我想为每次迭代提供一个像 [0 x 0]
这样的数组。但是,相反,它给了我以下错误:
The following error occurred converting from sym to double:
Error using mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.
Error in
Untitled123 (line 6)
y(1,j)=x*a(1,j);
有什么建议吗?
最佳答案
问题在于您将 y
分配为数字(浮点)数组,而不是符号数组。 a
是数字,但是当您将其乘以 x
时,结果将变为符号。由于 x 是符号变量而不是符号值,因此 Matlab 无法执行自动转换回 double 。您可以像这样分配y
:
y = sym(zeros(1,3));
关于arrays - Matlab中数值数组和符号数组的乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21080248/