如何交换矩阵中的两行(在 C 语言中)?
例如,给定一个矩阵:
1 2 3
4 5 6
7 8 9
如果你要交换 row[0] 和 row[1],结果矩阵将是:
4 5 6
1 2 3
7 8 9
你们可以帮忙吗我能得到一个 C 代码吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
例如,给定一个矩阵:
1 2 3
4 5 6
7 8 9
如果你要交换 row[0] 和 row[1],结果矩阵将是:
4 5 6
1 2 3
7 8 9
你们可以帮忙吗我能得到一个 C 代码吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
答案完全取决于你的“矩阵”是如何实现的,因为c语言没有这样的概念。
您使用的是二维数组吗?
或者其他什么?
二维数组
您必须手动移动各个元素。
(这里
r1
和r2
是已设置为要交换的两行的整数)或参见 James 的memcpy
实现 可能会更快但需要一整行的临时内存。参差不齐的数组
如果此操作非常常见,并且分析表明它消耗大量时间,则您可以考虑使用矩阵的参差不齐的数组实现。像这样的事情:
这个结构的有趣之处在于,您仍然可以使用
[][]
表示法访问它,但是行交换操作变得不规则数组从您的角度来看有两个缺点(嗯,三个导致内存管理麻烦的原因):它们需要额外的存储空间来存储行指针,并且不能使用内联初始化。
行结构
C 不支持以下形式的数组赋值;
但它确实支持结构的按值赋值语义。这为您提供了几个人建议的实现,但没有解释:
这很巧妙。它需要一整行内存,但如果编译器好的话可能会很快。最大的缺点是您无法再使用
[][]
语法来寻址各个矩阵元素。相反,你可以写m[i].r[j]
;其他
在c中实现“矩阵”的方法有很多很多,但它们大多更加复杂并且仅在特殊情况下有用。当您需要它们时,您将能够在每个问题的背景下自己回答这些问题。
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?
Or something else?
Two dimensional arrays
You will have to move individual elements by hand.
(here
r1
andr2
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:
The fun part about this structure is that you can still access it with the
[][]
notation, but the row swap operation becomesRagged 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;
but it does support by-value assignment semantics for structures. Which gives you the implementation that several people have suggested without explaining:
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 writem[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.
我可能会一次交换一个元素,以避免使用大量额外的存储空间。如果您主要处理诸如图形变换之类的事情,其中矩阵通常为 3x3 或 4x4,那么 James Curran 的方法可能会更好一些。如果您正在(或可能正在)处理非常大的矩阵,这将节省内存,并且很可能运行得更快:
I'd probably swap one element at a time to avoid using a lot of extra storage. If you're working primarily with things like graphics transforms where the matrices are typically 3x3 or 4x4, James Curran's approach is probably a bit better. If you are (or might be) working with really large matrices, this will save memory, and quite possibly run faster:
解决这个你的作业吗?
solve this your homework?
嘿!这是我关于堆栈溢出的第一篇文章,我知道它很长,希望我不会被禁止!
也许最优雅的方法之一是使用一个函数来交换两个接收到的参数 - 用它来交换矩阵分量。比如说swap(a,b)。
正如许多人已经说过的,我们应该考虑使用辅助变量
最近,我采用了一种新方法,我发现它令人印象深刻,使用按位异或运算(http://en.wikipedia.org/wiki/Xor)因此不需要辅助
您可以轻松地使用此操作来交换两个元素( a 和 b ) - 我相信这是题外话,但我坚持这个想法,因为我发现它很有趣。
最后,回答你的问题,你可以使用假设,
同时将矩阵声明为
你可以使用 XOR 方式交换行,首先确定需要交换的元素(根据行索引,正如你已经说过的)
我希望这对你进一步的练习很有用。
也试试这个例子,我相信你之后会更好地理解整个想法(不要忘记矩阵索引从 0 开始!)
Hy! this is my first post on stack overflow, I know it's pretty long, hope I won't get banned!
Probably one of the most elegant approaches would be using a function that swaps the two received arguments - using it to swap matrix components. Let's say somethig like swap(a,b).
As many have already said, we should consider using a auxiliary variable
Recently, I picked up a new method, which I found impressing, using bitwise XOR operation (http://en.wikipedia.org/wiki/Xor) thus a auxiliary is not needed
You can easily use this operation to swap two elements ( a and b ) - I belive this is off topic, but I insisted on this idea because I found it pretty interesting.
Finally, answering your question, you could use let's say
while having a matrix declared as
You can use your the XOR way the swap the rows, firstly identifyng the elements needed to be swapped ( according to row index, as you already said )
I hope this will be usefull in your further practice.
Also try this example, I'm sure you'll understand the whole idea much better afterwards (don't forget matrix index starts at 0 !)
有一个名为 swap 的函数:
There is a function called swap: