I have two matrix dynamically allocated.I want to copy all the data from the first matrix and center it by a border to the second one.Here is my code which apparently does not work properly so I would be grateful for some help:
我有两个动态分配的矩阵。我想把所有的数据从第一个矩阵拷贝到第二个矩阵中间。这是我的代码,显然不能正常工作,因此我将感谢一些帮助:
The first matrix:
第一个矩阵:
unsigned char ** sudo=(unsigned char **) malloc (width*sizeof (unsigned char*));
if ( sudo != NULL){
for (k=0; k<width ;k++){
sudo[k] =(unsigned char*) calloc (height,sizeof (unsigned char));
}
}
The second one:
第二个:
unsigned char ** fmatr=(unsigned char **) malloc ((width+border)*sizeof (unsigned char*));
if ( fmatr != NULL){
for (k=0; k<(width+border) ;k++){
fmatr[k] =(unsigned char*) calloc ((height+border),sizeof (unsigned char));
}
}
How I center first matrix in the middle of the second one:
我如何把第一矩阵放在第二点的中间
for(i=0,k=0;i<(width+border);i++,k++){
for(j=0,l=0;j<(height+border);j++,l++){
if((i>(2*border)) && (j>(2*border))){
fmatr[i][j]=sudo[k][l];
}
}
}
Any ideas?
什么好主意吗?
1 个解决方案
#1
0
Since the first matrix has width x height entries, you want to loop from 0 to width-1 (not width-1+border) and from 0 to height-1 (not height-1+border). Similarly, you want to add border/2
to the index of the second array when assigning.
由于第一个矩阵有宽度x高度条目,所以您希望从0到宽度-1(不是宽度-1+边框)以及从0到高度-1(不是高度-1+边框)进行循环。类似地,您希望在赋值时向第二个数组的索引添加border/2。
for(i=0;i<width;i++){
for(j=0;j<height;j++){
fmatr[i+(border/2)][j+(border/2)]=sudo[i][j];
}
}
#1
0
Since the first matrix has width x height entries, you want to loop from 0 to width-1 (not width-1+border) and from 0 to height-1 (not height-1+border). Similarly, you want to add border/2
to the index of the second array when assigning.
由于第一个矩阵有宽度x高度条目,所以您希望从0到宽度-1(不是宽度-1+边框)以及从0到高度-1(不是高度-1+边框)进行循环。类似地,您希望在赋值时向第二个数组的索引添加border/2。
for(i=0;i<width;i++){
for(j=0;j<height;j++){
fmatr[i+(border/2)][j+(border/2)]=sudo[i][j];
}
}