你怎么在(C语言)矩阵交换两行? [英] how do you swap two rows in a matrix (in C)?

查看:1066
本文介绍了你怎么在(C语言)矩阵交换两行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,给定的矩阵

1 2 3

4 5 6

7 8 9

如果您是goint交换行[0]和行[1],得到的矩阵将是:

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

4 5 6

1 2 3

7 8 9

你们可以帮我弄一个code C语言为这个?

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

推荐答案

答案完全取决于你如何黑客帝国的实施,因为C语言没有这样的事情的概念。

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

您使用二维数组?

double m[3][3];

或者其他什么东西?

Or something else?

您将不得不用手移动单个元素。

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;
}

(这里 R1 R2 是已设置为你希望交换的两排整数)或看<一个href=\"http://stackoverflow.com/questions/3550475/how-do-you-swap-two-rows-in-a-matrix-in-c/3550501#3550501\">James' 的memcpy 实施这可能会更快,但需要值得临时memeory的整个行。

(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.

如果这个操作是很常见的,并分析表明,它消耗了大量的时间,你可以考虑使用矩阵的一个衣衫褴褛的数组实现。事情是这样的:

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.

C不支持的阵列形式的分配;

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;

这是光滑。它需要存储器的整行,但如果编译器是任何好的可能是快的。最大的缺点是,你不能满足与 [] [] 语法单个矩阵元素了。而你写 M [] .R [J] ;

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];

有里用C实现一个矩阵许多,许多其他的方式,但他们大多是更为复杂和有用的,只有特殊情形。到时候你需要他们的时候,你就可以回答这个问题让自己在每个人的情况下。

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.

这篇关于你怎么在(C语言)矩阵交换两行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆