Mathematica中的循环问题

时间:2014-08-04 20:01:47

标签: wolfram-mathematica

我目前正在尝试在Mathematica中运行一个循环,该循环将尝试在一周中的日期插入日期列表的位置{i,4}。出于某种原因,dayintdate[[i,3]]!=date[[i-1,3]]的所有值都无法使date[[i,4]]=5增加。我非常感谢有关此问题的任何见解。

In[4]:= n = 344674; dayint = 5;

In[5]:= solardata = 
  Import["U:\\Masters Project\\Hobo \
Data\\SORMS_Landfill_Comparison_Input.csv", "csv"];

In[6]:= date = 
 Table[DateList[{ToString[solardata[[i, 1]]], {"Month", "Day", 
     "YearShort"}}], {i, n}];

In[8]:= date[[1, 4]] = 5;

In[14]:= For[i = 2, i < n + 1, i++, 
 If[date[[i, 3]] == date[[i - 1, 3]], date[[i, 4]] = dayint, 
  If[dayint == 7, dayint = 1, dayint++]; date[[i, 4]] = dayint]];

In[17]:= date;

1 个答案:

答案 0 :(得分:1)

似乎工作正常。

n = 3; dayint = 5;

date = {
   {2013, 11, 30, 0, 0, 0.},
   {2013, 11, 30, 0, 0, 0.},
   {2013, 12, 01, 0, 0, 0.}};

For[i = 2, i < n + 1, i++,
  If[date[[i, 3]] == date[[i - 1, 3]],
   date[[i, 4]] = dayint,
   If[dayint == 7, dayint = 1, dayint++];
   date[[i, 4]] = dayint]];

date
  

{{2013,11,30,0,0,0。},{2013,11,30,5,0,0。},{2013,12,1,6,     0,0。}}

修改

也许你错过了约会?

date = {
   {2013, 11, 28, 0, 0, 0.},
   {2013, 11, 29, 0, 0, 0.},
   {2013, 11, 29, 0, 0, 0.},
   {2013, 11, 30, 0, 0, 0.},
   {2013, 11, 30, 0, 0, 0.},
   {2013, 12, 01, 0, 0, 0.}};

dates = Union@date;
{mindate, maxdate} = Through[{First, Last}@dates];
days = QuantityMagnitude@DateDifference[mindate, maxdate, "Day"];
If[Length[dates] == days + 1, "All dates included", "There are dates missing"]
  

包括所有日期