1

我今天在Mathematica中遇到了这个错误:

Set::shape: "Lists {0,0,0,0,0,0,0,0,0,0} and {0,0,0,0,0,0,0,0,0,0,{1}} are not the same shape" >>

在其中 3 个之后:

General::stop : Further output of Set::shape will be suppressed during this calculation. >>

我很困惑为什么我不能将“1”附加到我的零列表中。这是因为我无法编辑传递给函数的列表吗?如果是这样,我如何编辑该列表并以某种方式返回或打印它?

这是我的完整代码:

notFunctioningFunction[list_] := (For[i = 1, i < 10, i++, list = Append[list, {1}]]; 
  Print[list])
list = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
notFunctioningFunction[list]

我附加“{1}”的原因是因为在我的函数中,我正在求解一个方程,并获取输出 {1} 的变量的值。这是我的代码:

varName / . Solve[ function1 == function2 ]

显然我是Mathematica的初学者,所以请耐心等待 :)

谢谢,
布科

4

2 回答 2

3

Append 需要一个列表和一个元素。像这样:

 Append[{1,2,3,4},5]

如果您有两个列表,则可以使用 Join。像这样:

 Join[{1,2,3,4},{5}]

这两个都会产生相同的结果:{1,2,3,4,5}。

于 2013-02-06T06:27:01.647 回答
2

亲爱的 Mathematica 初学者。

首先,当你使用类似的东西时

{a,b} = {c,d,e};

在 Mathematica 中,在两个列表之间,程序有困难,因为这是一个用于为变量赋值的构造,并且它要求(除其他外)两个列表相等。

如果您想要的只是将“1”添加到现有的命名列表中,一次一个,最好的构造是:

AppendTo[list, 1];

(此构造将修改变量“列表”)

或者

list = Join[list, {1}];

第二:关于错误消息,它们在评估中默认打印 3 次,然后静音,这样一长串相同的错误消息就不会弄乱你的显示。

第三,如果您需要将 10 个 1 添加到列表中,则无需在循环中构造它。您可以一次性完成:

list = Join[list, Table[1, {10}]]

或者,对初学者来说更神秘

list = Join[list, Array[1&, 10]]
于 2013-02-06T16:10:48.993 回答