在Matlab中创建二进制矩阵,报告二进制矩阵的数量不断增加 [英] Create binary matrix in Matlab reporting increasing number of ones
本文介绍了在Matlab中创建二进制矩阵,报告二进制矩阵的数量不断增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望您建议编写一个Matlab代码,以创建大小为31x5
的二进制矩阵A
I would like your advise to write a Matlab code that creates a binary matrix A
of size 31x5
such that
-
A
的第一行是[1 1 1 1 1]
从A
的2到6,每行只有一次1
from the 2nd to the 6th of A
we have 1
only once per row
[1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1]
从第7行到第16行,我们每行两次1
[1 1 0 0 0
1 0 1 0 0
1 0 0 1 0
...]
从第17行到第26行,每行有1
3次
从第26行到第31行,每行有1
四次
from the 26th to the 31th row we have 1
four times per row
我可以手动操作,但是我想知道是否有更快的方法进行操作.
I could that manually, but I would like to know if there is a faster way to proceed.
推荐答案
这里是一种方法:
- 生成所有可能的包含零和一的行,除了全零或全一;
- 根据行总和(然后按否定的行值)对行进行原子排序(原子上),以产生所需的顺序;
- 添加一行以生成结果.
N = 5;
A = dec2bin(1:2^N-2)-'0'; % step 1
[~, ind] = sortrows([sum(A,2) -A]); % step 2
result = [ones(1,N); A(ind,:)]; % step 3
这篇关于在Matlab中创建二进制矩阵,报告二进制矩阵的数量不断增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文