数学中的循环结构
感谢您回答之前的问题,我执行了以下操作,但我正在尝试执行此循环,但没有给出错误。我正在尝试做日期差异。
In[7] = Import["testA.txt", "Table" , "HeaderLines" -> 1]
Out[7] = {{100, 2010, 2, 20, 2010, 8, 30}, {110, 2010, 4, 30, 2010, 9,
12}, {112, 2010, 8, 20, 2010, 10, 28}}
In[10] = For[i = 1, i < 4,
i = i + 1, {a = Out[7] [[i, 2]], b = Out[7] [[i, 3]],
c = Out[7] [[i, 4]] , d = Out[7][[i, 5]], e = Out[7][[i, 6]],
f = Out[7][[i, 7]], DateDifference[{a, b, c}, {d, e, f}]}]
Thanks for answering the previous question had before, i did the following, but i am trying to execute this loop, but there is no error given. I am trying to do date difference.
In[7] = Import["testA.txt", "Table" , "HeaderLines" -> 1]
Out[7] = {{100, 2010, 2, 20, 2010, 8, 30}, {110, 2010, 4, 30, 2010, 9,
12}, {112, 2010, 8, 20, 2010, 10, 28}}
In[10] = For[i = 1, i < 4,
i = i + 1, {a = Out[7] [[i, 2]], b = Out[7] [[i, 3]],
c = Out[7] [[i, 4]] , d = Out[7][[i, 5]], e = Out[7][[i, 6]],
f = Out[7][[i, 7]], DateDifference[{a, b, c}, {d, e, f}]}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。 For[] 不生成输出。您想要计算的差异已被计算(191、135 和 69),并且结果没有写入或存储在任何地方。为了使这一点显而易见,请将 DateDifference[] 调用重写为
Print[DateDifference[{a,b,c},{d,e,f}]];
。没有任何关于您想要发生的事情的进一步提示。目前尚不清楚如何提供更多建议。你可以播种[]并收获[]。您可以将 DateDifference[] 的结果加入[]到结果列表中。您可以打印差异(如上所述)。您可以将结果分配给某些变量,定义一个符号来获取特定输入的值,等等......
- 编辑 -
哦,并解决其他一些问题受访者,此代码的正确形式是:
输出为:
{{2010, 2, 20, 2010, 8, 30, 191}, {2010, 4, 30, 2010, 9 , 12, 135}, {2010, 8, 20, 2010, 10, 28, 69}}
(您可能也想将其分配到某个地方)。Yes. For[] doesn't generate an output. The differences you wanted to computer were computed (191, 135, and 69) and the results weren't written or stored anywhere. To make this evident, rewrite your DateDifference[] call to
Print[DateDifference[{a,b,c},{d,e,f}]];
.Without any further hints about what you wanted to happen. It's not clear how to provide any more advice. You could Sow[] and Reap[]. You could Join[] the result of DateDifference[] to a results List. You could print the difference (as described above). You could assign the results to some variable(s), define a symbol to take their values of specific inputs, et c., et c., et c...
-- EDIT --
Oh, and to address some of the other respondents, the correct form of this code is:
Output is:
{{2010, 2, 20, 2010, 8, 30, 191}, {2010, 4, 30, 2010, 9, 12, 135}, {2010, 8, 20, 2010, 10, 28, 69}}
(and you probably want to assign that somewhere also).因为,您似乎想要单独处理每个列表元素,所以我会使用
Map
(或其缩写形式/@
),如下所示。Mathematica 主要是一种函数式编程语言,因此虽然存在像
For
这样的结构,但通常最好从对表达式进行操作和转换的函数角度进行思考。通常,如果您需要像For
这样的程序过程,Table
是更好的选择。我的代码有一个小小的缺点,缺少命名变量有时会很痛苦。然而,在这种情况下,它们不是必需的。但是,如果您需要它们,可以使用几种构造
与
,阻止
,模块
,甚至函数
。Since, you seem to want to process each list element individually, I'd use
Map
(or its short form/@
) for this, as follows.Mathematica is primarily a functional programming language, so while constructs like
For
exist, it is usually better to think in terms of functions operating on and transforming expressions. Often, if you need a procedural process likeFor
,Table
is a better bet.My code has one slight disadvantage, the lack of named variables can be a pain sometimes. In this case, however, they are not necessary. But, if you need them there are several constructs that could be used
With
,Block
,Module
, or evenFunction
.警告:这不是正确的方法
这是您的程序已更正并正在运行。我发布它是因为我在您的代码中看到了可能的概念错误,并且我认为拥有一个运行版本来与您的代码进行比较可能会帮助您识别它们并学习。
一些要点:
这是一个正在运行的代码:
但正如我所说,这不是可行的方法。阅读并研究(并在调试中运行!)rcollyer 的答案,以了解如何在 Mathematica 中正确执行此操作。
顺便说一句,您可以用 Table 替换 For 循环(请参阅 rcollyer 的建议)
Warning: This is not the way to go
This is your program corrected and running. I am posting it because I saw may conceptual errors in your code and I thought that having a running version for comparing it with yours may help you to identify them and learn.
Some points:
Here is a running code:
But as I said THIS IS NOT THE WAY TO GO. Read and study (and run in debug!) rcollyer's answer for understanding how to do it right in Mathematica.
BTW, You could replace the For loop by a Table (see rcollyer's suggestion)