数学中的循环结构

发布于 2024-10-01 01:54:01 字数 515 浏览 3 评论 0原文

感谢您回答之前的问题,我执行了以下操作,但我正在尝试执行此循环,但没有给出错误。我正在尝试做日期差异。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亢潮 2024-10-08 01:54:01

是的。 For[] 不生成输出。您想要计算的差异已被计算(191、135 和 69),并且结果没有写入或存储在任何地方。为了使这一点显而易见,请将 DateDifference[] 调用重写为 Print[DateDifference[{a,b,c},{d,e,f}]];

没有任何关于您想要发生的事情的进一步提示。目前尚不清楚如何提供更多建议。你可以播种[]并收获[]。您可以将 DateDifference[] 的结果加入[]到结果列表中。您可以打印差异(如上所述)。您可以将结果分配给某些变量,定义一个符号来获取特定输入的值,等等......

- 编辑 -

哦,并解决其他一些问题受访者,此代码的正确形式是:

    foo = Import["testA.txt", "Table" , "HeaderLines" -> 1];
    diff[in_List] := Join[in[[Range[2, 7]]], {DateDifference[in[[Range[2, 4]]], in[[Range[5, 7]]]]}]
    diff /@ foo

输出为:{{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:

    foo = Import["testA.txt", "Table" , "HeaderLines" -> 1];
    diff[in_List] := Join[in[[Range[2, 7]]], {DateDifference[in[[Range[2, 4]]], in[[Range[5, 7]]]]}]
    diff /@ foo

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

杀手六號 2024-10-08 01:54:01

因为,您似乎想要单独处理每个列表元素,所以我会使用 Map(或其缩写形式/@),如下所示。

In[1]:=data = {{100, 2010, 2, 20, 2010, 8, 30}, 
               {110, 2010, 4, 30, 2010, 9, 12}, 
               {112, 2010, 8, 20, 2010, 10, 28}};
       #~Join~{DateDifference[#[[2;;4]],#[[5;;]]]}& /@ data
Out[1]:={191, 135, 69}

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.

In[1]:=data = {{100, 2010, 2, 20, 2010, 8, 30}, 
               {110, 2010, 4, 30, 2010, 9, 12}, 
               {112, 2010, 8, 20, 2010, 10, 28}};
       #~Join~{DateDifference[#[[2;;4]],#[[5;;]]]}& /@ data
Out[1]:={191, 135, 69}

Mathematica is primarily a functional programming language, so while constructs like Forexist, it is usually better to think in terms of functions operating on and transforming expressions. Often, if you need a procedural process like For, 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 even Function.

心欲静而疯不止 2024-10-08 01:54:01

警告:这不是正确的方法

这是您的程序已更正并正在运行。我发布它是因为我在您的代码中看到了可能的概念错误,并且我认为拥有一个运行版本来与您的代码进行比较可能会帮助您识别它们并学习。

一些要点:

  • 使用常见结构(Length@x、i++ 等)
  • 不要使用“Out[n]=” ... 切勿使用大写字母作为变量名的第一个字符
  • 了解如何构造列表(请参阅 AppendTo)

这是一个正在运行的代码:

x = Import["testA.txt", "Table" , "HeaderLines" -> 1]

ret = {};
For[i = 1, i <= Length@x, i++,
  AppendTo[ret,
    {a = x[[i, 2]],
     b = x[[i, 3]],
     c = x[[i, 4]],
     d = x[[i, 5]],
     e = x[[i, 6]],
     f = x[[i, 7]],
     DateDifference[{a, b, c}, {d, e, f}]}
    ];
  ];
Print@ret  

但正如我所说,这不是可行的方法。阅读并研究(并在调试中运行!)rcollyer 的答案,以了解如何在 Mathematica 中正确执行此操作。

顺便说一句,您可以用 Table 替换 For 循环(请参阅 rcollyer 的建议)

Table[
      {a = x[[i, 2]],
       b = x[[i, 3]],
       c = x[[i, 4]],
       d = x[[i, 5]],
       e = x[[i, 6]],
       f = x[[i, 7]],
       DateDifference[{a, b, c}, {d, e, f}]}, {i, Length@x}]

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:

  • Use of common constructs (Length@x, i++, etc)
  • Don't use "Out[n]=" ... Never use capitals as first char of a variable name
  • Learn how to construct lists (see the AppendTo)

Here is a running code:

x = Import["testA.txt", "Table" , "HeaderLines" -> 1]

ret = {};
For[i = 1, i <= Length@x, i++,
  AppendTo[ret,
    {a = x[[i, 2]],
     b = x[[i, 3]],
     c = x[[i, 4]],
     d = x[[i, 5]],
     e = x[[i, 6]],
     f = x[[i, 7]],
     DateDifference[{a, b, c}, {d, e, f}]}
    ];
  ];
Print@ret  

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)

Table[
      {a = x[[i, 2]],
       b = x[[i, 3]],
       c = x[[i, 4]],
       d = x[[i, 5]],
       e = x[[i, 6]],
       f = x[[i, 7]],
       DateDifference[{a, b, c}, {d, e, f}]}, {i, Length@x}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文