【发布时间】:2013-05-07 21:14:53
【问题描述】:
给定:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
我想将二维数组(struct MATRIX)拆分成一个 struct MATRIX 数组 给定一个块大小的 CS: 假设 cs 为 2, 答案是
Seg[0]:
1 2
1 2
1 2
Seg[1]:
3 4
3 4
3 4
....
Seg[3]:
7 8
7 8
7 8
这是我的矩阵结构:
typedef struct MATRIX {
int nrow;
int ncol;
int **element;
} MATRIX;
这里是分隔它们的函数:
void SegmentMatrix(MATRIX input,MATRIX* segs,int Chunksize, int p) {
int i,j,r;
//Allocate segs
for (i = 0; i<p;i++)
{
CreateMatrix(&(segs[i]),input.nrow ,Chunksize,0);
}
//Now Copy the elements from input to the segs
//where seg0 takes from 0 to cs cols of a, and all their rows, and seg1 takes from cs to 2cs ...
printf("Stats:\n\t P: %d\t CS: %d\n",p,Chunksize);
for (r = 0; r<p; r++) {
for (i = 0; i<input.nrow;i++) {
for (j = r*Chunksize; j<r*Chunksize+Chunksize-1; j++) {
//I tried (&(segs[r]))->element... Doesn't work, produces wrong data
segs[r].element[i][j] = input.element[i][j];
}
}
PRINTM(segs[r]);
}
}
请注意,PRINTM 基本上打印矩阵,它通过检查 segs[r].nrow 和 ncol 知道限制 CreateMatrix 从内部获取以下输入(&matrix、行数、列数、填充类型)和 malloc。
filltype:
0- generates zeroth matrix
1- generates identity
else A[i][j] = j; for simplicity
问题在于,如果我打印矩阵 Segs[i],它们都会返回 CreateMatrix 给出的默认值,而不是新添加的值。
澄清: 好的,所以如果你们检查 SegmentMatrix 函数中的最后一个 PRINTM,它会输出矩阵,就好像没有发生 for 循环一样,我可以删除 for 循环并获得相同的输出.. 我在这一行做错了吗(取自 SegmentMatrix)
Segs[r].element[i][j] = input.element[i][j];
【问题讨论】:
-
您会在哪里调用 PRINTM 以显示错误输入?我希望在您的上述代码中看到这一点,以确保它不是马类型问题之前的推车。
-
如果您查看 SegmentMatrix 中的最后一条语句,您将看到 PRINTM,它显示了 segs 的默认值,就好像整个 for 循环没有发生一样
-
@MichaelDorgan 希望能解释一下