Transpose matrix (w\ pointers)

Reply

Join Date: Dec 2005
Posts: 2
Reputation: agabriel is an unknown quantity at this point 
Solved Threads: 0
agabriel agabriel is offline Offline
Newbie Poster

Transpose matrix (w\ pointers)

 
0
  #1
Dec 2nd, 2005
Hello,
Okay I'm stumped and can't figure out why this code chunk isn't working properly (works on square matrixs; everything else doesn't work)....

uint8 * imageTranspose( uint8 *im ){
uint8 *im_transpose;
int x, y, org_offset, tran_offset;
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;
}

At the end of the day I will not be using Matlab and therefore I'm just using it as a prototyping tool. Does anyone have any ideas by chance?

Thanks,
Anthony
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Transpose matrix (w\ pointers)

 
0
  #2
Dec 2nd, 2005
I might do a transpose like this.
#include <stdio.h>
#include <stdlib.h>

int **create    (                                size_t rows, size_t cols );
void  destroy   (       int             **array, size_t rows);
int **transpose ( const int *const *const array, size_t rows, size_t cols);
void  print     ( const int *const *const array, size_t rows, size_t cols );

int main(void)
{
   size_t rows = 3, cols = 5;
   int **array = create(rows, cols);
   int **trans = transpose(array, rows, cols);
   print(array, rows, cols);
   print(trans, cols, rows);
   destroy(array, rows);
   destroy(trans, cols);
   return 0;
}

int **create(size_t rows, size_t cols)
{
   size_t r, c;
   int k = 0;
   int **array = malloc(rows * sizeof *array);
   if ( array )
   {
      for ( r = 0; r < rows; ++r )
      {
         array[r] = malloc(cols * sizeof *array[r]);
         if ( !array[r] )
         {
            destroy(array, r - 1);
            return NULL;
         }
         for ( c = 0; c < cols; ++c )
         {
            array[r][c] = k++;
         }
      }
   }
   return array;
}

int **transpose(const int *const *const array, size_t rows, size_t cols)
{
   size_t r, c;
   int **result = malloc(cols * sizeof *result);
   if ( result )
   {
      for ( r = 0; r < cols; ++r )
      {
         result[r] = malloc(rows * sizeof *result[r]);
         if ( !result[r] )
         {
            destroy(result, r - 1);
            return NULL;
         }
         for ( c = 0; c < rows; ++c )
         {
            result[r][c] = array[c][r];
         }
      }
   }
   return result;
}

void destroy(int **array, size_t rows)
{
   size_t r;
   for ( r = 0; r < rows; ++r )
   {
      free(array[r]);
   }
   free(array);
}

void print(const int *const *const array, size_t rows, size_t cols)
{
   size_t r, c;
   for ( r = 0; r < rows; ++r )
   {
      for ( c = 0; c < cols; ++c )
      {
         printf("%2d ", array[r][c]);
      }
      putchar('\n');
   }
   putchar('\n');
}

/* my output
 0  1  2  3  4
 5  6  7  8  9
10 11 12 13 14

 0  5 10
 1  6 11
 2  7 12
 3  8 13
 4  9 14
*/
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 2
Reputation: agabriel is an unknown quantity at this point 
Solved Threads: 0
agabriel agabriel is offline Offline
Newbie Poster

Re: Transpose matrix (w\ pointers)

 
0
  #3
Dec 2nd, 2005
I like that - that looks good.

Thanks,
Anthony
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 1
Reputation: sumantra529 is an unknown quantity at this point 
Solved Threads: 0
sumantra529 sumantra529 is offline Offline
Newbie Poster

Re: Transpose matrix (w\ pointers)

 
0
  #4
May 17th, 2008
100% correct matrix Transpose program with out any pointer without any complicity.
-------------------------------------------------------------------
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a[10][10],i,j,n,m,save;
  6. clrscr();
  7. printf("Enter The Rows And Cloumns And Of The First Matrix:");
  8. scanf("%d%d",&m,&n);
  9. printf("\nEnter Elements Of The First Matrix:\n");
  10. for(i=0;i<m;i++)
  11. {
  12. for(j=0;j< n;j++)
  13. {
  14. scanf("%d",&a[i][j]);
  15. }
  16. }
  17.  
  18. printf("The First Matrix Is:\n");
  19. for(i=0;i<m;i++)
  20. {
  21. for(j=0;j<n;j++)
  22. {
  23. printf("%d",a[i][j]); //print the first matrix
  24. }
  25. printf("\n");
  26. }
  27. for (i = 0; i < 4; i++)
  28. {
  29. for (j = 0; j < 4; j++)
  30. {
  31. save = a[i][j];
  32. a[i][j] = a[j][i];
  33. a[j][i] = save;
  34. }
  35. }
  36. printf("After inverse of Matrix Is:\n");
  37. for(i=0;i<m;i++)
  38. {
  39. for(j=0;j<n;j++)
  40. {
  41. printf("%d",a[j][i]);
  42. }
  43. printf("\n");
  44. }
  45. getch();
  46. }
Last edited by Ancient Dragon; May 17th, 2008 at 1:10 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Transpose matrix (w\ pointers)

 
0
  #5
May 17th, 2008
Nice job reviving a 2 year old thread?
Last edited by Clockowl; May 17th, 2008 at 1:57 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC