Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Slightly OT: Transpose a matrix in C



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




BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org