memcpy将3D数组的列转换为1D数组

问题描述 投票:2回答:2

我有一个3D数组mat,我需要提取第一列并复制到一维数组arr

我已经尝试了下面的代码,我可以将第一行mat复制到arr,但我需要将mat的第一列复制到arr。有没有更好的方法来做到这一点,而不是逐个元素复制?

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

int main()
{
int mat[4][2][4] = {{{1,2,3,4},{10,20,30,40}},{{11,12,13,14},{110,120,130,140}},{{21,22,23,24},{210,220,230,240}},{{31,32,33,34},{310,320,330,340}}};
int arr[4];
int index,idx;

for(index=0;index<4;index++){
    memcpy(arr, &mat[index][0], sizeof(arr));
    for(idx=0;idx<4;idx++){
        printf("%d\t",arr[idx]);
    }
    printf("\n");
}

return 0;
}

实际结果:

1   2   3   4   
11  12  13  14  
21  22  23  24  
31  32  33  34

预期成绩:

1       10
11      110
21      210
31      310

*更新

c
2个回答
3
投票

如果我很清楚你想要那个:

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

int main()
{
  int mat[4][2][4] = {{{1,2,3,4},{10,20,30,40}},{{11,12,13,14},{110,120,130,140}},{{21,22,23,24},{210,220,230,240}},{{31,32,33,34},{310,320,330,340}}};
  int arr[2];
  int index,idx;

  for (index=0; index<4; ++index) {
    /* only set */
    for (idx=0; idx<2; ++idx) {
      arr[idx] = mat[index][idx][0];
    }

    /* only print */
    for (idx=0; idx<2; ++idx) {
      printf("%d\t", arr[idx]);
    }
    printf("\n");
  }

  return 0;
}

执行产生:

1   10  
11  110 
21  210 
31  310 

我故意将作业和印刷循环分开,即使它们是相同的


但是对于最后一个维度只使用索引0很奇怪,所以我认为你实际上想要:

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

int main()
{
  int mat[4][2][4] = {{{1,2,3,4},{10,20,30,40}},{{11,12,13,14},{110,120,130,140}},{{21,22,23,24},{210,220,230,240}},{{31,32,33,34},{310,320,330,340}}};
  int arr[2];
  int idx0, idx1, idx2;

  for (idx2 = 0; idx2 != 4; ++idx2) {
    for (idx0=0; idx0<4; ++idx0) {
      /* only set */
      for (idx1=0;idx1<2;idx1++) {
        arr[idx1] = mat[idx0][idx1][idx2];
      }

      /* only print */
      for (idx1=0;idx1<2;idx1++) {
        printf("%d\t", arr[idx1]);
      }
      printf("\n");
    }
  }

  return 0;
}

执行产生:

1   10  
11  110 
21  210 
31  310 
2   20  
12  120 
22  220 
32  320 
3   30  
13  130 
23  230 
33  330 
4   40  
14  140 
24  240 
34  340

0
投票

如果要使用memcpy,则数据必须是连续的。我将尝试使用以下代码解释它。

第一部分:我用你的矩阵向你展示内存中的数据顺序。

第二部分:我重新排序数据(Rq:为了测试和理解目的,我认为,每个维度最好有不同数量的元素。例如[3] [2] [4]以避免一些混淆。不过,我不要改变它来保存你的数据)

第三部分:我像你一样使用memcpy。

我有个问题。您是否尝试将一些代码从fortran调整为C?

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

int main()
{
//First part
int mat[4][2][4]={{{1,2,3,4},{10,20,30,40}},{{11,12,13,14},{110,120,130,140}},{{21,22,23,24},{210,220,230,240}},{{31,32,33,34},{310,320,330,340}}};
int *ptr=mat;
int i=0;
for (int z=0; z<4; z++) for (int y=0; y<2; y++) for (int x=0; x<4; x++) printf("mat(%d,%d,%d)=%d\n",x,y,z,ptr[i++]);

printf("\n------------------------\n\n");

//Second part
/* int mat[4][2][4]=
    {{{1,2,3,4},            {10,20,30,40}},
    {{11,12,13,14},         {110,120,130,140}},
    {{21,22,23,24},         {210,220,230,240}},
    {{31,32,33,34},         {310,320,330,340}}}; */
// Written like that to show "a sort of matrix transposition" (do not take this words mathematically)of left and right part.

int mat1[4][2][4]={
    {{1,11,21,31},          {10,110,210,310}},
    {{2,12,22,32},          {20,120,220,320}},
    {{3,13,23,33},          {30,130,230,330}},
    {{4,14,24,34},          {40,140,230,340}}
};

int *ptr1=mat1;
i=0;
for (int z=0; z<4; z++) for (int y=0; y<2; y++) for (int x=0; x<4; x++) printf("mat(%d,%d,%d)=%d\n",x,y,z,ptr1[i++]);

//Third part
int arr[4];
int index,idx;

for(index=0;index<2;index++){
    memcpy(arr, &mat1[0][index], sizeof(arr));
    for(idx=0;idx<4;idx++){
        printf("%d\t",arr[idx]);
    }
    printf("\n");
}

return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.