Slightly OT: Transpose a matrix in C

Anthony Gabrielson agabriel at home.tzo.org
Fri Dec 2 16:04:48 EST 2005


Ken,
	Your right that was actually a typo.  It also seems to be working.  
I had that code hours ago, why it didn't seem to be working is beyond 
me...

Thanks,
Anthony

On Fri, 2 Dec 2005, Ken Gosier wrote:

> One question to ask is, should tran_offset be set as:
> 
> tran_offset= y+(x*dims[1]);
> 
> If there's a minus sign as below, it seems like it would lead to a segfault.
> 
> 
> 
> Anthony Gabrielson wrote:
> > 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
> > _______________________________________________
> > Discuss mailing list
> > Discuss at blu.org
> > http://olduvai.blu.org/mailman/listinfo/discuss
> >
> 
> 
> -- 
> Ken Gosier
> ken at kg293.net
> ken_gosier at yahoo.com
> _______________________________________________
> Discuss mailing list
> Discuss at blu.org
> http://olduvai.blu.org/mailman/listinfo/discuss
> 



More information about the Discuss mailing list