在 C++ 中求矩阵的最大值、最小值、平均值

发布于 2024-10-16 20:31:22 字数 90 浏览 1 评论 0原文

如何在 C++ 中找到给定矩阵 (matrix[i][j]) 中的最大值、最小值和平均值。类型为 unsigned long double。

How do I find the maximum, minimum and average values in a given matrix (matrix[i][j]) in C++. The type is unsigned long double.

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

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

发布评论

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

评论(4

二货你真萌 2024-10-23 20:31:22

这里没有什么聪明的事情可以做(只是伪代码,因为这闻起来像硬件):

for each entry in the matrix:
    add the entry to a running sum
    compare the entry to a running min
        if it's smaller, it's the new running min
    compare the entry to a running max
        if it's larger, it's the new running max
average is the sum divided by the number of entries

您可以对该循环进行微优化以提高或降低其效率,但是您无法在算法上做任何事情来提高效率聪明的。无论如何,您都需要查看所有 i*j 条目。

There's nothing clever to be done here (pseudocode only, since this smells like HW):

for each entry in the matrix:
    add the entry to a running sum
    compare the entry to a running min
        if it's smaller, it's the new running min
    compare the entry to a running max
        if it's larger, it's the new running max
average is the sum divided by the number of entries

You can micro-optimize that loop to make it more or less efficient, but there's nothing you can do algorithmically to be more clever. You need to look at all i*j entries no matter what.

王权女流氓 2024-10-23 20:31:22

也许是这样的:

最大值

int maximum = 0;
for(int x=0; x<width; ++x)
  for(int y=0; y<height; ++y)
    maximum = std::max(matrix[x][y], maximum);

最小值

int minimum = 0;
for(int x=0; x<width; ++x)
  for(int y=0; y<height; ++y)
    minimum = std::min(matrix[x][y], minimum);

平均

int avarage = 0;
for(int x=0; x<width; ++x)
  for(int y=0; y<height; ++y)
    avarge += matrix[x][y];
avarge /= width*height;

Maybe this:

Maximum:

int maximum = 0;
for(int x=0; x<width; ++x)
  for(int y=0; y<height; ++y)
    maximum = std::max(matrix[x][y], maximum);

Minimum:

int minimum = 0;
for(int x=0; x<width; ++x)
  for(int y=0; y<height; ++y)
    minimum = std::min(matrix[x][y], minimum);

Avarage:

int avarage = 0;
for(int x=0; x<width; ++x)
  for(int y=0; y<height; ++y)
    avarge += matrix[x][y];
avarge /= width*height;
萌辣 2024-10-23 20:31:22

假设矩阵是一个实际的 C++ 二维数组,您可以使用标准算法吗?

未经测试的代码:

long double mean = std::accumulate(matrix[0], matrix[0] + i*j, 0.0) / (i*j);
long double matrix_min = std::min_element(matrix[0], matrix[0] + i*j);
long double matrix_max = std::max_element(matrix[0], matrix[0] + i*j);

请注意,这会对矩阵进行额外的传递,以便清楚地了解它在做什么。

如果它是另一种容器类型,例如向量的向量,那么您必须在每一行上运行算法并获取每一行的最大值。

Assuming matrix is an actual C++ two-dimensional array could you use standard algorithms.

Untested code:

long double mean = std::accumulate(matrix[0], matrix[0] + i*j, 0.0) / (i*j);
long double matrix_min = std::min_element(matrix[0], matrix[0] + i*j);
long double matrix_max = std::max_element(matrix[0], matrix[0] + i*j);

Do note that this does extra passes over the matrix at the benefit of being clear what it's doing.

If it's another container type like a vector of vectors then you'd have to run the algorithms on each row and take the max of each row.

深海不蓝 2024-10-23 20:31:22

循环遍历所有值,记录当前最大值、最小值和累积和。然后将累积和除以元素数量即可得到平均值。

Loop through all the values, recording current max, min, and cumulative sum. Then divide the cumulative sum by the number of elements to get the mean.

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