2

I need N variables for my equation system:

X = cell(N,1)
for k=1:N
    X(k) = {sym('X(k)')};
end

After creating these variables I want to utilize them in an equation system:

for i=1:N
    for j=1:N
        if i~=j
            S(i)=sum(X(j))
        end
    end
    f(i)=x(i)+2*S(i)+3
end

I get the error Undefined function 'sum' for input arguments type 'cell'. How should I define the variables X(1),...X(N) without using 'cell'?

4

2 回答 2

2

cell2mat您可以使用该函数 将它们转换为矩阵。http://www.mathworks.com/help/matlab/ref/cell2mat.html

于 2013-06-10T15:16:55.900 回答
2

根据sym文档,您可以使用例如。A = sym('A%d%d', [2 2]);创建一个符号矩阵。

你是这个意思吗?

N = 5;
% Initialize symbolic matrices with proper size
X = sym('x%d', [N 1]);
S = sym(zeros(N, 1));
f = sym(zeros(N, 1));

for i=1:N
    for j=1:N
        if i~=j
            S(i) = S(i) + X(j);
        end
    end
    f(i)=X(i)+2*S(i)+3;
end
于 2013-06-10T15:18:13.797 回答