【问题标题】:Finding minimum temperature for a month in a data set在数据集中查找一个月的最低温度
【发布时间】:2018-08-29 23:57:25
【问题描述】:

我已经制作了一个函数,可以从给定月份和年份的特定时间的数据集中提取所有温度。

看起来像exctractperiod(data, year, month, time)

我现在想找出某个月份(例如 1 月)的最低温度,跨越多年。例如,如果我查看 1997 年和 2006 年之间的一月。现在我想要 1997 年至 2006 年之间一月的最低记录温度。

到目前为止我的进步(请记住,这只是我想要的一个粗略的想法)

for i = 1:12
  for z = 1:x+1

    year=startyear:1:endyear;
    year(z)

   p = extractperiodtwo(DATA, year, month, time);    

end

我想知道如何编写我的for 循环,这样对于month 1,它会经历 1997-2006 年并找到最低温度。然后对于下一个循环,它会经历 1997-2006 年的 month 2。然后应该重复到第 12 个月。

变量p 存储年YYYY 月MM 的所有温度。

不要把我的程序当回事,这只是一个粗略的写法,让我自己对它的外观有一个想法。也许它澄清了我的问题。

【问题讨论】:

    标签: matlab for-loop


    【解决方案1】:

    您可能正在寻找这样的东西:

    mintemp = inf(1,12); % initialize to infinity for each month
    for month = 1:12
      for year = 1997:2006
        p = extractperiodtwo(DATA, year, month, time);
        temp = min(p); % assuming `p` contains multiple temperatures?
        mintemp(month) = min(mintemp(month), temp); % update current month's min temp
      end
    end
    

    【讨论】:

    • 为什么需要mintemp = inf(1,12);?关于你写的循环。那么,如果我从流程图的角度来看,这将如何执行?这是否会从 1997 年的 1:12 月份开始,下一个循环将在 1998 年的 1:12 月份开始,依此类推?这将如何进行?
    • @JonnyCola:将其复制粘贴到 MATLAB 编辑器中,然后使用调试器逐步完成。你会学到很多我无法解释的东西。第一个for循环分配month=1,然后下一行分配year=1997,然后这些值用于获得p。当执行到第一个end时,它会回到对应的for,并分配year=1998,然后得到下一个p。等等。所以对于每个月,它会贯穿所有年份。
    • mintemp 的初始化是必要的,否则mintemp(month) = min(mintemp(month), temp) 将毫无意义:您正在使用那里的现有值,因此您需要先给它一个值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 2015-09-30
    • 1970-01-01
    相关资源
    最近更新 更多