找到你要的答案

Q:How do I create random matrix where each column is all zeroes except for one random row?

Q:我如何创建中的每一列都是零,除了一个随机行随机矩阵?

I want to create a matrix of size m-by-n where all elements in a column are 0 except one element which is 1. That one element must be at a random position.

eg.

[0 1 0 0 0
 0 0 1 0 0
 1 0 0 1 0
 0 0 0 0 0 
 0 0 0 0 1]

我想创建一个大小为M-和-n在一列中的所有元素都是0元,除了一个1矩阵。一个元素必须处于随机位置。

如.

[0 1 0 0 0
 0 0 1 0 0
 1 0 0 1 0
 0 0 0 0 0 
 0 0 0 0 1]
answer1: 回答1:

Another idea I have is to create the identity matrix of size m x m, then use randi with a range from 1 up to m to create a vector of n elements long. After, you'd use this vector to access the columns of the identity matrix to complete the random matrix you desire:

m = 5; n = 5; %// Given your example
M = eye(m);
out = M(:,randi(m, n, 1));

Here's one possible run of the above code:

out =

     1     0     0     0     0
     0     0     0     0     0
     0     0     0     1     0
     0     0     0     0     0
     0     1     1     0     1

另一个想法我是创建尺寸M×M的身份矩阵,然后用兰迪从1到m创建n个元素的向量距离长。之后,您将使用此向量访问身份矩阵的列来完成您想要的随机矩阵:

m = 5; n = 5; %// Given your example
M = eye(m);
out = M(:,randi(m, n, 1));

这里有一个可能运行的上述代码:

out =

     1     0     0     0     0
     0     0     0     0     0
     0     0     0     1     0
     0     0     0     0     0
     0     1     1     0     1
answer2: 回答2:

To add some variety, here's another approach:

m = 4;
n = 5;
[~, result] = sort(rand(m,n));
result = double(result==1);

This gives, for example,

result =
     0     0     0     0     1
     0     1     0     0     0
     1     0     0     1     0
     0     0     1     0     0

添加一些品种,这里的另一种方法:

m = 4;
n = 5;
[~, result] = sort(rand(m,n));
result = double(result==1);

这给出,例如,

result =
     0     0     0     0     1
     0     1     0     0     0
     1     0     0     1     0
     0     0     1     0     0
answer3: 回答3:

You can also use rand and max to do the job:

m=4;
n=5;
R=rand(m,n);
result = bsxfun(@eq, R, max(R,[],1))

On my machine it gave:

 1     1     0     0     0
 0     0     0     0     0
 0     0     1     0     1
 0     0     0     1     0

How it works: Generating a random matrix, R, and then setting to 1 the entry corresponding to the maximal element at each column. No need for sorting.


Regarding the original answer of Divakar, since it uses randperm it is restricted to square matrix only, and it will only produce random permutation matrices.
One possible way to correct his solution is to use randi instead of randperm:

result = bsxfun( @eq, (1:m)', randi(m, 1, n ) )

May give this output:

 1     0     1     0     0
 0     0     0     0     0
 0     0     0     0     0
 0     1     0     1     1

As for the answer of bla, using accumarry can save the use of zeros and sub2ind:

m=5; n=10;
R=randi(m,n,1);
A = accumarray( {R, (1:n)' }, 1, [m n] )

May give this output:

 0     0     0     0     1     0     0     1     0     0
 0     1     0     0     0     0     1     0     1     0
 1     0     0     1     0     0     0     0     0     1
 0     0     0     0     0     1     0     0     0     0
 0     0     1     0     0     0     0     0     0     0

您也可以使用兰德和MAX来做这项工作:

m=4;
n=5;
R=rand(m,n);
result = bsxfun(@eq, R, max(R,[],1))

在我的机器上:

 1     1     0     0     0
 0     0     0     0     0
 0     0     1     0     1
 0     0     0     1     0

它是如何工作的:生成一个随机矩阵,R,然后设置为1对应于每个列的最大元素的条目。不需要分类。


Regarding the original answer of Divakar, since it uses randperm it is restricted to square matrix only, and it will only produce random permutation matrices.
One possible way to correct his solution is to use randi instead of randperm:

result = bsxfun( @eq, (1:m)', randi(m, 1, n ) )

可以给这个输出:

 1     0     1     0     0
 0     0     0     0     0
 0     0     0     0     0
 0     1     0     1     1

对于BLA的答案,用accumarry可以节省零点和sub2ind使用:

m=5; n=10;
R=randi(m,n,1);
A = accumarray( {R, (1:n)' }, 1, [m n] )

可以给这个输出:

 0     0     0     0     1     0     0     1     0     0
 0     1     0     0     0     0     1     0     1     0
 1     0     0     1     0     0     0     0     0     1
 0     0     0     0     0     1     0     0     0     0
 0     0     1     0     0     0     0     0     0     0
answer4: 回答4:

here's an example using randi:

m=5; n=10;
A=zeros(m,n);
R=randi(m,n,1);
A(sub2ind(size(A),R',1:n))=1


A =

 0     0     0     0     0     0     0     1     0     1
 0     0     1     0     0     0     0     0     0     0
 0     1     0     1     0     1     0     0     0     0
 0     0     0     0     1     0     0     0     0     0
 1     0     0     0     0     0     1     0     1     0

这里有一个例子用兰迪:

m=5; n=10;
A=zeros(m,n);
R=randi(m,n,1);
A(sub2ind(size(A),R',1:n))=1


A =

 0     0     0     0     0     0     0     1     0     1
 0     0     1     0     0     0     0     0     0     0
 0     1     0     1     0     1     0     0     0     0
 0     0     0     0     1     0     0     0     0     0
 1     0     0     0     0     0     1     0     1     0
answer5: 回答5:

You can use sparse with randi for a one-liner, like so -

full(sparse(randi(m,1,n),1:n,1,m,n))

Sample run -

>> m = 5; n = 6;
>> full(sparse(randi(m,1,n),1:n,1,m,n))
ans =
     0     1     0     0     0     1
     0     0     1     1     0     0
     0     0     0     0     0     0
     1     0     0     0     1     0
     0     0     0     0     0     0

你可以使用稀疏,兰迪一一套,像这样—

full(sparse(randi(m,1,n),1:n,1,m,n))

运行示例—

>> m = 5; n = 6;
>> full(sparse(randi(m,1,n),1:n,1,m,n))
ans =
     0     1     0     0     0     1
     0     0     1     1     0     0
     0     0     0     0     0     0
     1     0     0     0     1     0
     0     0     0     0     0     0
matlab  matrix  vectorization