【发布时间】:2015-01-26 18:32:50
【问题描述】:
我正在尝试对特定风暴和多个风暴中所有观测的飓风观测之间的连续小时数(变量“hrs”)进行累积求和,以确定风暴的持续时间。这就是我所拥有的:
stormid = [188, 188, 288, 288, 288, 388, 488, 488, 588...] %numbers represent a unique
% code identifying a particular storm (i.e. 188= 1st storm of 1988)
hrs = [0,6,0,6,6,0,0,6,0...] %hours between observations in each storm
% where 0 indicates the start of a storm (this corresponds with the stormid above)
我的目标是总结每个风暴 id 的小时数,所以我认为使用 accumarray 会起作用,但它没有。这是我尝试过的。
duration= accumarray(stormid, hrs, []);
然后我也尝试了 cumsum,但我无法弄清楚如何仅在风暴 id 相同时使用 cumsum。
如果有人有任何想法,我将不胜感激!谢谢。
【问题讨论】:
-
我想不出一个巧妙的方法。
accumarray只能与返回标量而非向量的函数一起使用,因此不能与cumsum一起使用。您是否可以边走边手动循环遍历stormid和hrs,每次找到新的 ID 号时重新开始? -
那么
duration= accumarray(stormid, hrs, []);的错误消息是什么意思?Second input VAL must be a vector with one element for each行in SUBS, or a scalar.。如果您只是阅读doc accumarray的第二个示例,您可能会解决这个转置问题。 -
作为旁注:由于您的stormIDs不是连续的,而不是使用带有列向量的
accumarray,您应该使用sparse,它也可以对值求和,但会节省一些内存(或者您可以使用unique在预处理步骤中转换 ID)。 -
@knedlsepp- 没有错误消息,当我期望一个 283 x 1 向量(数据集中的风暴数)和输出时,结果只是给了我一个 3105x1 向量感觉。不过,根据你们所有人的建议, accumarray 将不起作用。感谢您的建议,我会尝试您的建议。
-
@knedlsepp:我的数据实际上是按列而不是按行排列的,我只是为了方便而写的
标签: matlab cumsum accumarray