2014-01-12 41 views
2

一个象征性的阵列我有以下代码:乘法数字数组,并在Matlab

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); 

有什么建议吗?

回答

2

问题是,您将y分配为数字(浮点)数组,而不是符号数组。 a是数字,但是当您乘以x时,结果变为符号。由于x是一个符号变量而不是符号值,因此Matlab无法执行自动转换回双精度。您可以分配y这样的:

y = sym(zeros(1,3)); 
+0

对不起朋友,但是你在上面提供的线索,解决不了我的problem.As我说,我想有一个像[0 X 0]数组,其中x是每次迭代的符号变量,例如,第一次迭代[x 0 0],第二次迭代[0 x 0]和第三次迭代[0 0 x] ... –

+0

@MRMeyqani:是的。这是'y'(你的第三行代码)的分配,而不是'for'循环中的部分。请仔细阅读我的答案。 – horchler

+0

好的,我明白了。 你对循环部分有什么建议吗? –