使用贴图查找向量中的多种模式

发布于 2025-01-08 01:40:22 字数 1010 浏览 0 评论 0原文

所以我已经研究这个模式有一段时间了,并得到了一些帮助来弄清楚如何在这里做到这一点,并认为我可能需要更多的帮助。我已经弄清楚这么多了。

map<int,unsigned> frequencyCount;
//This is my attempt to increment the values of the map everytime one of the same numebers 
for(size_t i = 0; i < v.size(); ++i)
    frequencyCount[v[i]]++;

unsigned currentMax = 0;
unsigned checked = 0;
unsigned mode = 0;
for(auto it = frequencyCount.cbegin();
    it != frequencyCount.cend(); ++it )

if (it ->second > currentMax)
    {
        mode = it->first;
        currentMax = it->second;
    }

if (currentMax == 1)
{
    cout << "There is no mode in the vector" << endl;
}

else {
cout << " The mode of the vector is: " << mode << endl;
}

因此,

它将返回向量中最常出现的 int,并且如果没有一个映射值超过 1,则返回没有模式。现在我一直在尝试找出在不止一种模式的情况下该怎么办, 前任。 1 2 3 3 4 4 5 当前返回 3。我想返回 3 和 4。

从逻辑上讲,我会说 if 语句检查最频繁出现的变量与第二频繁出现的变量是可行的。当然,如果测试数据足够大的话,400种模式也是有可能的。因此,我需要创建一个循环,当当前变量不再等于出现频率低于它的变量时,该循环将进行检查并退出。我只是真的不知道该怎么做。关于从哪里开始有什么建议吗?

So I've been at this mode thing for a while, and got some help figuring out just how to do it on here and thought I might ask for a little more assitance. I've got this much figured out.

map<int,unsigned> frequencyCount;
//This is my attempt to increment the values of the map everytime one of the same numebers 
for(size_t i = 0; i < v.size(); ++i)
    frequencyCount[v[i]]++;

unsigned currentMax = 0;
unsigned checked = 0;
unsigned mode = 0;
for(auto it = frequencyCount.cbegin();
    it != frequencyCount.cend(); ++it )

if (it ->second > currentMax)
    {
        mode = it->first;
        currentMax = it->second;
    }

if (currentMax == 1)
{
    cout << "There is no mode in the vector" << endl;
}

else {
cout << " The mode of the vector is: " << mode << endl;
}

}

So it will reutrn the most frequently occuring int within the vector, and return that there is no mode if the none of the map values exceed 1. Now I've been trying to figure out what to in the case of more than one mode, ex. 1 2 3 3 4 4 5 currently returns 3. I would like to reutrn 3 and 4.

Logically I would say an if statement checking the variable which occurs most frequently against the second most frequent would work. Of course there is the possibility of 400 modes if the test data is large enough though. So I would need to create a loop the goes through checking and exits when the current variable is no longer equal to the one that occurs one less frequently than it. I'm just really not sure how to go about that. Any advice on where to get started?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

时光匆匆的小流年 2025-01-15 01:40:22

正如您所指出的,该模式不一定是唯一的。

因此,正如您所正确指出的那样,您应该以一组数字的形式提供答案。

我将创建一个向量来存储您的集合中的一组独特模式,然后输出模式向量的内容。

例如

std::vector<float> Modes;

// 3 is detected as mode as it occurs 3 times in your set
Modes.push_back(3);

// 4 is also detected as mode, since it also occurs 3 times in your set
Modes.push_back(4);

// output the modes for the set
std::cout << "the following mode(s) were detected in the set";
for( int n = 0; n < Modes.size(); n ++ )
{
  std::cout << "Mode: " << Modes[n] << "\n";
}

希望这是有道理的

As you point out, the mode is not necessarily unique.

So, you should supply the answer as a set of numbers, as you've correctly stated.

I would create a vector to store the set of unique modes from your set, then output the content of the mode vector.

e.g.

std::vector<float> Modes;

// 3 is detected as mode as it occurs 3 times in your set
Modes.push_back(3);

// 4 is also detected as mode, since it also occurs 3 times in your set
Modes.push_back(4);

// output the modes for the set
std::cout << "the following mode(s) were detected in the set";
for( int n = 0; n < Modes.size(); n ++ )
{
  std::cout << "Mode: " << Modes[n] << "\n";
}

hope this makes sense

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文