简体   繁体   English

如何生成随机二进制矩阵,其中每行中只有一位的值为1?

[英]How to generate a random binary matrix where only one bit in each row has value 1?

I want to use MATLAB to generate a random binary matrix A (nxm) which satisfies a condition: 我想用MATLAB生成满足条件的随机二进制矩阵A (nxm):

Each row contains one position with value 1 . 每行包含一个值为1位置。 Other positions are value 0 . 其他职位为0 The position having 1 value is random position. 具有1值的位置是随机位置。

I tried this code 我试过这段代码

   n=5;m=10; 
   %% A = randi([0 1], n,m);
   A=zeros(n,m);
   for i=1:n
       rand_pos=randperm(m);
       pos_one=rand_pos(1); % random possition of 1 value
       A(i,pos_one)=1;
   end

Is it correct? 这是正确的吗?

The solution works, but it is inefficient. 解决方案有效,但效率低下。
You are using randperm to create a vector (array), and then use only the first element of the vector. 您正在使用randperm创建向量(数组),然后仅使用向量的第一个元素。
You can use randi to create a scalar (single element) instead: 您可以使用randi来创建标量(单个元素):

n=5;m=10; 
A=zeros(n,m);
for i=1:m
    %rand_pos gets a random number in range [1, n].
    rand_pos = randi([1, n]);
    A(rand_pos, i)=1;
end

You can also use the following "vectorized" solution: 您还可以使用以下“矢量化”解决方案:

rand_pos_vec = randi([1, n], 1, m);
A(sub2ind([n, m], rand_pos_vec, 1:m)) = 1;

The above solution: 以上解决方案:

  • Creates a vector of random values in range [1, n]. 创建范围[1,n]中随机值的向量。
  • Use sub2ind to convert "row index" to "matrix index". 使用sub2ind将“行索引”转换为“矩阵索引”。
  • Place 1 in "matrix index". 将1放在“矩阵索引”中。

You can do it in one line using bsxfun and randi : 你可以使用bsxfunrandi在一行中randi

A = double(bsxfun(@eq, 1:m, randi(n, n, 1)));

This compares the row vector [1 2 ... m] with an n × 1 random vector of values from 1 to n . 这将行向量[1 2 ... m]与值为1nn × 1随机向量进行比较。 The comparison is done element-wise with singleton expansion. 通过单独扩展进行元素比较。 For each row, exactly one of the values of [1 2 ... m] equals that in the random vector. 对于每一行, [1 2 ... m]的值中的一个恰好等于随机向量中的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MATLAB:如何生成每行和每列具有特定数量 1 的随机二进制矩阵? - MATLAB : How can I generate a random binary matrix with a specific number of 1s in each row and in each column? 如何创建随机矩阵,其中每列都是零,除了一个随机行? - How do I create random matrix where each column is all zeroes except for one random row? 如何在Matlab中生成一个随机矩阵,每个值都重复两次? - How to generate a random matrix in matlab with each value reapating twice? 构造一个二进制矩阵,使得每一列仅包含单个“ 1”,并且每一行的总和具有期望的值 - construct a binary matrix such that each column contains only single “1” and sum of each row is of desired value MATLAB:生成二进制矩阵的所有可能组合,每列中只有一个“ 1” - MATLAB: Generate all possible combination of a binary matrix with only one '1' in each column 如何生成所有可能的二进制nxm矩阵,其中每一行的总和为1 - How to generate all posible binary nxm matrices, where the sum of each row is 1 如何在matlab中生成具有特定条件的随机二进制矩阵? - how to generate a random binary matrix with a specific condition in matlab? 如何使用随机条目生成矩阵并对行和列进行约束? - How to generate a matrix with random entries and with constraints on row and columns? 生成具有特定行数的二进制矩阵 - Generate binary matrix with specific number of ones in row 保存在每个循环中创建的矩阵值保存在公共矩阵中,每个循环矩阵具有相同的行大小,但列数 - save matrix value which create in each loop save in common matrix ,each loop matrix has same row size but column is ow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM