gpt4 book ai didi

Matlab:与固定值相同的索引向量值

转载 作者:行者123 更新时间:2023-12-02 04:38:28 25 4
gpt4 key购买 nike

我有一个包含事件日期数的 1×1000 向量,我想计算每个日期的事件总数(本例已简化)。 datesevents 的维度一致。

我的代码是

   i = 0
for d = unique(dates)
i = i + 1
result(i) = length(events(d == dates))
end

我发现 d == dates 的维度不匹配。我明白为什么(d 是一个 1×1 向量),但我该如何正确编写它?

奖励积分:i 的解决方案非常难看...提示?

谢谢!


按要求编辑:dates 包含 datenums

  729028
729028
729028
729028
729028

事件包含 float :

0.1205
0.2932
2.0384
2.0384
1.0411
0.5425

最佳答案

问题在于 unique(dates) 是一个列向量,而 for 遍历等号右侧的任何列。因此,d 是一个向量,而不是原始代码中的标量。

获取代码你想做什么:

for d = unique(dates)'

为了避免循环:

d = hist(dates,unique(dates));

关于Matlab:与固定值相同的索引向量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21410802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com