Slightly OT: Transpose a matrix in C
Anthony Gabrielson
agabriel at home.tzo.org
Fri Dec 2 15:32:53 EST 2005
Hello,
Sorry this is OT... I'm trying to transpose a 2D matrix in C and
its being a bit tough. My code works great for squares but everything
else failes. Here is the function:
uint8 * imageTranspose( uint8 *im ){
uint8 *im_transpose;
int x, y, org_offset, tran_offset;
//dims[0] is X length and dims[1] is Y length. dims is global
int im_size = dims[0] * dims[1];
if ( (im_transpose = malloc(sizeof(uint16) * im_size)) == NULL )
mexErrMsgTxt("trans im malloc failed...\n");
//Transpose
for(x=0;x<dims[0]; x++){
for(y=0;y<dims[1]; y++){
org_offset = x+(y*dims[0]); //So X=X & Y=Y
tran_offset= y-(x*dims[1]); //So X=Y & Y=X
*(im_transpose+tran_offset) = *(im+org_offset);
//mexPrintf("%d\t%d\n",*(im+org_offset),*(im_transpose+tran_offset));
}
}
return im_transpose;
}
So this function works well with square arrays but doesn't work with non
square arrays. I'm not sure why and I can't find any reference on the
web. Any help would be appreciated.
Thanks,
Anthony
More information about the Discuss
mailing list