我有一个矩阵(inA),其中包含0到23之间的值。
图像0的数据是:0,2,4,6,...
图像1的数据是:1,3,5,7 ......
我想通过每个图像将dat重新组织到另一个矩阵(ouB)。 所以,ouB必须是:0,2,4,6,8,10,12,14,16 ......,1,3,5,7,9 ...
现在ouB是:0,2,4,6,1,3,5,7,8,10,12 ......
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[]){
int NbOfElmts = 4 , NbOfTech = 3 , NbOfImages = 2;
int *inA = malloc( NbOfElmts * NbOfTech * NbOfImages * sizeof( *inA ) );
int *ouB = malloc( NbOfElmts * NbOfTech * NbOfImages * sizeof( *ouB ) );
//fill inA
for ( int i = 0; i < NbOfTech; i++ )
{
for ( int j = 0; j < NbOfElmts; j++ )
{
for ( int k = 0; k < NbOfImages; k++ )
{
inA[ i * NbOfElmts * NbOfImages + j * NbOfImages + k ] = i * NbOfElmts * NbOfImages + j * NbOfImages + k;
}
printf( "\n Data for #0 image: %d ",i * NbOfElmts * NbOfImages + j * NbOfImages );
printf( "\n Data for #1 image: %d ",i * NbOfElmts * NbOfImages + j * NbOfImages + 1 );
}
}
// print inA
for ( int i = 0; i < NbOfTech; i++ )
{
for ( int j = 0; j < NbOfElmts; j++ )
{
for ( int k = 0; k < NbOfImages; k++ )
{
printf("\nA = %d",inA[ i * NbOfElmts * NbOfImages + j * NbOfImages + k ] );
}
}
}
printf("\n");
// copy inA elements to ouB organized per Image
for ( int i = 0; i < NbOfTech; i++ )
{
for ( int j = 0; j < NbOfElmts; j++ )
{
for ( int k = 0; k < NbOfImages; k++ )
{
ouB[ i * NbOfElmts * NbOfImages + k * NbOfElmts + j ] = inA[ i * NbOfElmts * NbOfImages + j * NbOfImages + k ];
//ouB[ i * NbOfElmts * NbOfImages + j * NbOfImages + k ] = inA[ i * NbOfElmts * NbOfImages + k * NbOfElmts + j ];
}
}
}
// print ouB
for ( int i = 0 ; i < NbOfElmts * NbOfTech * NbOfImages; i++ )
printf( "\nB = %d",ouB[ i ] );
printf("\n");
free ( inA );
free ( ouB );
return 0;
}
整个问题在这里:
ouB[ i * NbOfElmts * NbOfImages + k * NbOfElmts + j ] = inA[ i * NbOfElmts * NbOfImages + j * NbOfImages + k ];
答案 0 :(得分:2)
尝试:
ouB[ k*NbOfElmts*NbOfTech + i * NbOfElmts + j ] = inA[ i * NbOfElmts * NbOfImages + j * NbOfImages + k ];
而不是:
ouB[ i * NbOfElmts * NbOfImages + k * NbOfElmts + j ] = inA[ i * NbOfElmts * NbOfImages + j * NbOfImages + k