Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you swap two rows in a matrix (in C)?

Tags:

c

matrix

row

swap

for example, given a matrix:

1 2 3

4 5 6

7 8 9

if you are goint to swap row[0] and row[1], resulting matrix would be:

4 5 6

1 2 3

7 8 9

can you guys help me get a code in C for this?

like image 262
user420360 Avatar asked Aug 23 '10 18:08

user420360


2 Answers

The answer depends entirely on how your "matrix" is implemented, because the c language has no notion of such a thing.

Are you using two dimensional arrays?

double m[3][3];

Or something else?

Two dimensional arrays

You will have to move individual elements by hand.

for (i=0; i<ROWLENGTH; ++i){
  double temp;
  temp = m[r2][i];
  m[r2][i] = m[r1][i];
  m[r1][i] = temp;
}

(here r1 and r2 are ints that have been set to the two row you want to swap) or see James' memcpy implementation which may well be faster but requires a whole rows worth of temporary memeory.

Ragged Arrays

If this operation is very common and profiling reveals that it is consuming a lot of time, you might consider using a ragged array implementation of the matrix. Something like this:

double **m;
m = malloc(sizeof(double*)*NUMROWS);
/* put error checking here */
for (i=0; i<NUMROWS; ++i){
  m[i] = malloc(sizeof(double)*ROWLENGTH);
  /* error checking again */
}

The fun part about this structure is that you can still access it with the [][] notation, but the row swap operation becomes

double *temp;
temp = m[r2];
m[r2] = m[r1];
m[r1] = temp;

Ragged arrays have two disadvantages from your point of view (well, three 'cause of the memory management hassle): they require extra storage for the row pointers, and you can't use inline initialization.

Row-as-astructure

C does not support array assignments of the form;

double r[3], q[3] = { 1, 2, 3 };
r = q; /* ERROR */

but it does support by-value assignment semantics for structures. Which gives you the implementation that several people have suggested without explaining:

typedef struct { double r[ROWLENGTH] } row;
row m[NUMROWS] = { {1, 2, 3}, {4, 5, 6}, {7, 8 9}};

row temp = m[2];
m[2] = m[1];
m[1] = temp;

which is slick. It requires a whole row of memory, but if the compiler is any good is probably fast. The big disadvantage is that you can not address individual matrix elements with the [][] syntax anymore. Rather you write m[i].r[j];

Others

There are many, many other ways to implement a "matrix" in c, but they are mostly much more complicated and useful only in specialized situations. By the time you need them you'll be able to answer this questions for yourself in the context of each one.

like image 122
dmckee --- ex-moderator kitten Avatar answered Oct 26 '22 19:10

dmckee --- ex-moderator kitten


typedef int Row[3];
Row Matrix[3];

Row Temp;

memcpy(Temp, Matrix[0], sizeof(Row));
memcpy(Matrix[0], Matrix[1], sizeof(Row));
memcpy(Matrix[1], Temp, sizeof(Row));
like image 20
James Curran Avatar answered Oct 26 '22 19:10

James Curran