如何重新排列该矩阵的列?
给定一个二进制矩阵,其中每行和每列只包含一个 1,我需要按列重新排列矩阵,使其成为单位矩阵。例如,给定一个二进制矩阵:
Binary = [ 0 1 0 0 0
0 0 1 0 0
1 0 0 0 0
0 0 0 0 1
0 0 0 1 0 ]
为了获得单位矩阵,我们将列重新排列为 2 3 1 5 4
。
对于任何给定的任意二进制二元矩阵,我们如何以最佳方式重新排列列?
Given a binary matrix in which every row and column contains exactly only one 1, I need to rearrange the matrix columnwise so that it will become an identity matrix. For example, given a binary matrix:
Binary = [ 0 1 0 0 0
0 0 1 0 0
1 0 0 0 0
0 0 0 0 1
0 0 0 1 0 ]
To get the identity matrix we rearrange the column as 2 3 1 5 4
.
How can we optimally rearrange the columns for any given arbitrary square binary matrix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个非常简单的方法是使用函数 FIND ,如下所示:
您可以按如下方式测试它是否工作:
旧方法:
这不像上述解决方案那么紧凑或高效,但您也可以转置矩阵并使用SORTROWS 对列进行排序(现在转置为行)并返回排序索引。这实际上会按升序对值进行排序,这将为您提供一个反对角矩阵,因此您需要使用 FLIPUD。这是代码:
A very simple way to do this is to use the function FIND like so:
And you can test that it work as follows:
OLD APPROACH:
This isn't as compact or efficient as the above solution, but you could also transpose the matrix and use SORTROWS to sort the columns (now transposed into the rows) and return the sort indices. This will actually sort values in ascending order, which will give you an anti-diagonal matrix, so you will want to flip the vector of indices using FLIPUD. Here's the code:
如果您知道矩阵可以转换为单位矩阵,为什么不创建一个具有相同维度的单位矩阵呢?
If you know the matrix can be manipulated into an identity matrix, why don't you just create an identity matrix with the same dimensions?