如何在C中使用零填充和取消填充2D矩阵?

时间:2021-03-24 21:33:58

I have a matrix of dimensions N by N.

我有一个尺寸为N乘N的矩阵。

Ex:  1 2 3 
     4 5 6
     7 8 9

I need to obtain matrix of dimensions N+2 by N+2.

我需要通过N + 2获得尺寸为N + 2的矩阵。

Ex O/P -  0 0 0 0 0
          0 1 2 3 0
          0 4 5 6 0
          0 7 8 9 0
          0 0 0 0 0

And then I also need to be able to extract the actual matrix if given only the padded matrix. How can I do this?

然后,如果只给出填充矩阵,我还需要能够提取实际矩阵。我怎样才能做到这一点?

1 个解决方案

#1


1  

Using pointers and pointer math it's easy to achieve this. I will assume you already have access to the matrix dimension.

使用指针和指针数学很容易实现这一点。我假设您已经可以访问矩阵维度。

The below code allows you to pad and de-pad your matrix. I've tested with different matrix sizes so it should work with any square matrix:

以下代码允许您填充和取消填充矩阵。我已经测试了不同的矩阵大小,所以它应该适用于任何方阵:

#include <stdio.h>
#include <stdlib.h>

void pad(int *s,int *d,int dim);
void depad(int *s,int *d,int dim);
void prnt(int *s,int dim);

int main(void)
{
    int v[3][3]={{1,2,3},{4,5,6},{7,8,9}}; //example matrix
    int dim=3; //get dimension from program
    int *t;
    int *dpad;

    //mem alloc
    t = (int*) calloc((dim+2)*(dim+2),sizeof(int));
    dpad = (int*) calloc(dim*dim,sizeof(int));

    //--------------------------------------------
    printf("Initial matrix:\n");
    prnt(*v,dim);

    //--------------------------------------------
    printf("Padded matrix:\n");
    pad(*v,t,dim);
    prnt(t,dim+2);

    //--------------------------------------------
    printf("Depadded matrix:\n");
    depad(t,dpad,dim+2);
    prnt(dpad,dim);

    //free mem and return
    free(t);
    free(dpad);
    return 0;
}

void pad(int *s,int*d,int dim)
{
    int i,j;
    for(i=0;i<dim;i++)
        for(j=0;j<dim;j++)
            *(d+(i*(dim+2)+(dim+3+j)))=*(s+i*dim+j);
}

void depad(int *s,int *d,int dim)
{
    int i,j;
    dim=dim-2;
    for(i=0;i<dim;i++)
        for(j=0;j<dim;j++)
            *(d+i*dim+j)=*(s+(i*(dim+2)+(dim+3+j)));
}

void prnt(int *s,int dim)
{
    int i,j;
    for(i=0;i<dim;i++)
    {
        for(j=0;j<dim;j++) printf("%d ",*(s+dim*i+j));
        printf("\n");
    }   
}

#1


1  

Using pointers and pointer math it's easy to achieve this. I will assume you already have access to the matrix dimension.

使用指针和指针数学很容易实现这一点。我假设您已经可以访问矩阵维度。

The below code allows you to pad and de-pad your matrix. I've tested with different matrix sizes so it should work with any square matrix:

以下代码允许您填充和取消填充矩阵。我已经测试了不同的矩阵大小,所以它应该适用于任何方阵:

#include <stdio.h>
#include <stdlib.h>

void pad(int *s,int *d,int dim);
void depad(int *s,int *d,int dim);
void prnt(int *s,int dim);

int main(void)
{
    int v[3][3]={{1,2,3},{4,5,6},{7,8,9}}; //example matrix
    int dim=3; //get dimension from program
    int *t;
    int *dpad;

    //mem alloc
    t = (int*) calloc((dim+2)*(dim+2),sizeof(int));
    dpad = (int*) calloc(dim*dim,sizeof(int));

    //--------------------------------------------
    printf("Initial matrix:\n");
    prnt(*v,dim);

    //--------------------------------------------
    printf("Padded matrix:\n");
    pad(*v,t,dim);
    prnt(t,dim+2);

    //--------------------------------------------
    printf("Depadded matrix:\n");
    depad(t,dpad,dim+2);
    prnt(dpad,dim);

    //free mem and return
    free(t);
    free(dpad);
    return 0;
}

void pad(int *s,int*d,int dim)
{
    int i,j;
    for(i=0;i<dim;i++)
        for(j=0;j<dim;j++)
            *(d+(i*(dim+2)+(dim+3+j)))=*(s+i*dim+j);
}

void depad(int *s,int *d,int dim)
{
    int i,j;
    dim=dim-2;
    for(i=0;i<dim;i++)
        for(j=0;j<dim;j++)
            *(d+i*dim+j)=*(s+(i*(dim+2)+(dim+3+j)));
}

void prnt(int *s,int dim)
{
    int i,j;
    for(i=0;i<dim;i++)
    {
        for(j=0;j<dim;j++) printf("%d ",*(s+dim*i+j));
        printf("\n");
    }   
}