【问题标题】:Increasing the size of a matrix增加矩阵的大小
【发布时间】:2017-06-17 16:00:58
【问题描述】:

我在第一行有 3 个整数,前两个是矩阵的 N 和 M 行和列,X 是我必须放大矩阵的次数。 在接下来的 N 行中,我有 M 个元素,更准确地说是一个矩阵,我必须“放大”。

这项工作是用四个for 语句完成的,但我不知道该怎么做。

输入

2 2 3
1 2
3 4

输出

1 1 1 2 2 2
1 1 1 2 2 2 
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4 

输出是给定的矩阵,其大小增加了 X。

我的代码

    void ZoomIn(int n, int m, int x, int a[][100], int aux[][100])
{
    int i, j, ind_i, I, J, ind_j;


    if (x == 0)
        return ;
        I = J = 0;
    for(i = 0; i < n; i++)
        {
        for(j = 0; j < n; j++)
            {

              for(ind_i = J; ind_i < x; ind_i++)
                for(ind_j = I; ind_j < x; ind_j++)
                    aux[ind_i][ind_j] = a[i][j]; // the first element in the smallest matrix
            I = I + x;
            }
}

如何正确增加IJ 的值,以便创建更小的子矩阵? 我看到这个过程的方式是 我必须创建

 1 1 1
 1 1 1
 1 1 1

然后继续下一个小矩阵

 2 2 2
 2 2 2
 2 2 2

【问题讨论】:

  • 现在问题好多了。使用返回类型void 并且没有传递指针来保存结果,当函数返回时,您在此函数中创建的任何矩阵都将不复存在。这不可能。
  • 你可以利用整数除法的两个循环来做到这一点,例如for(i=0;i&lt;outputSize;i++) printf("inputIndex=%d outputIndex=%d\n", i/3, i); 但我同意@JohnColeman 的观点,你需要解决的第一个问题是内存管理问题。
  • 我现在已经修复了矩阵问题并将其作为参数传递,问题是我必须将元素保存在该矩阵中
  • 最好的方法是进行一次试运行并查看每次正确创建子矩阵所需的索引值。然后看看你可以在你的代码中做什么来获得这些索引

标签: c arrays matrix zooming


【解决方案1】:

如果您可以使用 C99(或 C11)可变长度数组,那么这是迄今为止最简单的。只要确保数组对于堆栈来说不会太大,您可以直接在main() 中分配局部变量,然后将数组传递给ZoomIn() 进行处理。

您建议您应该使用四个嵌套循环。这当然有效。外面的一对嵌套循环遍历基本(未缩放)数组中的元素;内部的一对嵌套循环将基本数组的当前元素复制到缩放数组的相关小节中。

此代码使用在stderr.h 中声明并在stderr.c 中定义的函数——代码可从https://github.com/jleffler/soq/tree/master/src/libsoq 获得。这些函数极大地简化了错误报告。

#include <stdio.h>
#include <string.h>
#include "stderr.h"

static void dump_array(const char *tag, int n, int m, int array[n][m])
{
    printf("%s (%dx%d):\n", tag, n, m);
    for (int i = 0; i < n; i++)
    {
        const char *pad = "";
        for (int j = 0; j < m; j++)
        {
            printf("%s%d", pad, array[i][j]);
            pad = " ";
        }
        putchar('\n');
    }
}

static void ZoomIn(int r, int c, int base[r][c], int z, int zoom[z * r][z * c])
{
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            for (int k = z * i; k < z * (i + 1); k++)
            {
                for (int l = z * j; l < z * (j + 1); l++)
                    zoom[k][l] = base[i][j];
            }
        }
    }
}

int main(int argc, char **argv)
{
    err_setarg0(argv[0]);
    if (argc > 1)
        err_usage("");

    char buffer[4096];

    if (fgets(buffer, sizeof(buffer), stdin) == 0)
        err_error("Unexpected EOF\n");
    int r, c, z;
    buffer[strcspn(buffer, "\n")] = '\0';
    if (sscanf(buffer, "%d%d%d", &r, &c, &z) != 3)
        err_error("Expected 3 numbers on first line, not [%s]\n", buffer);
    if (r <= 0 || r > 100 || c <= 0 || c > 100 || z <= 0 || z > 100)
        err_error("Matrix size out of control (r = %d, c = %d, z = %d)\n", r, c, z);
    if (r * c * z * z > 1000000)
        err_error("Zoomed matrix too big (r = %d, c = %d, z = %d)\n", r, c, z);

    int base[r][c];
    for (int i = 0; i < r; i++)
    {
        if (fgets(buffer, sizeof(buffer), stdin) == 0)
            err_error("Unexpected EOF 2\n");
        buffer[strcspn(buffer, "\n")] = '\0';
        int offset = 0;
        for (int j = 0; j < c; j++)
        {
            int p;
            if (sscanf(buffer + offset, "%d%n", &base[i][j], &p) != 1)
                err_error("Format error on line [%s]\n", buffer);
            offset += p;
        }
    }
    dump_array("Base Array", r, c, base);

    int zoom[r*z][c*z];

    ZoomIn(r, c, base, z, zoom);

    dump_array("Zoomed Array", r * z, c * z, zoom);

    return 0;
}

给定数据文件:

2 2 3
1 2
3 4

输出是:

Base Array (2x2):
1 2
3 4
Zoomed Array (6x6):
1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4

给定数据文件:

4 5 6
1 3 5 7 9
2 4 6 8 0
0 1 2 3 4
5 6 7 8 9

输出是:

Base Array (4x5):
1 3 5 7 9
2 4 6 8 0
0 1 2 3 4
5 6 7 8 9
Zoomed Array (24x30):
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9
2 2 2 2 2 2 4 4 4 4 4 4 6 6 6 6 6 6 8 8 8 8 8 8 0 0 0 0 0 0
2 2 2 2 2 2 4 4 4 4 4 4 6 6 6 6 6 6 8 8 8 8 8 8 0 0 0 0 0 0
2 2 2 2 2 2 4 4 4 4 4 4 6 6 6 6 6 6 8 8 8 8 8 8 0 0 0 0 0 0
2 2 2 2 2 2 4 4 4 4 4 4 6 6 6 6 6 6 8 8 8 8 8 8 0 0 0 0 0 0
2 2 2 2 2 2 4 4 4 4 4 4 6 6 6 6 6 6 8 8 8 8 8 8 0 0 0 0 0 0
2 2 2 2 2 2 4 4 4 4 4 4 6 6 6 6 6 6 8 8 8 8 8 8 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9

您也可以仅使用 2 个循环来编写缩放代码,但每次迭代需要两次除法:

static void ZoomIn(int r, int c, int base[r][c], int z, int zoom[z * r][z * c])
{
    for (int k = 0; k < z * r; k++)
    {
        for (int l = 0; l < z * c; l++)
            zoom[k][l] = base[k/z][l/z];
    }
}

这会产生与四重嵌套循环版本相同的输出。尽管代码更简洁,但不清楚它是否会比四重嵌套循环版本更快。

【讨论】:

  • 谢谢!现在我明白我的问题是什么了。
猜你喜欢
  • 1970-01-01
  • 2018-08-03
  • 2020-01-06
  • 2021-05-11
  • 2013-12-20
  • 2011-04-19
  • 1970-01-01
  • 2019-01-29
  • 2012-12-11
相关资源
最近更新 更多