如何重新排列该矩阵的列?

发布于 2024-11-13 10:31:58 字数 371 浏览 0 评论 0原文

给定一个二进制矩阵,其中每行和每列只包含一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留蓝 2024-11-20 10:31:58

一个非常简单的方法是使用函数 FIND ,如下所示:

[index,~] = find(Binary.');  %'# Transpose the matrix and find the row indices
                              %#   of the non-zero entries

您可以按如下方式测试它是否工作:

>> Binary(:,index)

ans =

     1     0     0     0     0    %# Yup, that's an identity matrix alright!
     0     1     0     0     0
     0     0     1     0     0
     0     0     0     1     0
     0     0     0     0     1

旧方法:

这不像上述解决方案那么紧凑或高效,但您也可以转置矩阵并使用SORTROWS 对列进行排序(现在转置为行)并返回排序索引。这实际上会按升序对值进行排序,这将为您提供一个反对角矩阵,因此您需要使用 FLIPUD。这是代码:

[~,index] = sortrows(Binary.');  %'# Transpose and sort the matrix
index = flipud(index);            %# Flip the index vector

A very simple way to do this is to use the function FIND like so:

[index,~] = find(Binary.');  %'# Transpose the matrix and find the row indices
                              %#   of the non-zero entries

And you can test that it work as follows:

>> Binary(:,index)

ans =

     1     0     0     0     0    %# Yup, that's an identity matrix alright!
     0     1     0     0     0
     0     0     1     0     0
     0     0     0     1     0
     0     0     0     0     1

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:

[~,index] = sortrows(Binary.');  %'# Transpose and sort the matrix
index = flipud(index);            %# Flip the index vector
涫野音 2024-11-20 10:31:58

如果您知道矩阵可以转换为单位矩阵,为什么不创建一个具有相同维度的单位矩阵呢?

identity_matrix=eye(length(Binary))

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?

identity_matrix=eye(length(Binary))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文