954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Transpose matrix (w\ pointers)

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

agabriel
Newbie Poster
2 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

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 )
         {
            <strong>result[r][c] = array[c][r];</strong>
         }
      }
   }
   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
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

I like that - that looks good.

Thanks,
Anthony

agabriel
Newbie Poster
2 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

100% correct matrix Transpose program with out any pointer without any complicity.
-------------------------------------------------------------------

#include<stdio.h>
#include<conio.h>
void main()
{
	int a[10][10],i,j,n,m,save;
	clrscr();
	printf("Enter The Rows And Cloumns And Of The First Matrix:");
	scanf("%d%d",&m,&n);
	printf("\nEnter Elements Of The First Matrix:\n");
	for(i=0;i<m;i++)
	{
		for(j=0;j< n;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
	
	printf("The First Matrix Is:\n");
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			printf("%d",a[i][j]); //print the first matrix
		}
		printf("\n");
	}
	for (i = 0; i < 4; i++)
	{
		 for (j = 0; j < 4; j++)
		{
			save = a[i][j];
			a[i][j] = a[j][i];
			a[j][i] = save;
		}
	}
	printf("After inverse of Matrix Is:\n");
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			printf("%d",a[j][i]);
		}
		printf("\n");
	}
	getch();
}
sumantra529
Newbie Poster
1 post since May 2008
Reputation Points: 10
Solved Threads: 0
 

Nice job reviving a 2 year old thread? ;)

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You