generate random binary matrix under a condition
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Amira Akra
am 13 Mär. 2015
Beantwortet: Andrei Bobrov
am 13 Mär. 2015
I want to generate a matrix with values that are either zero or one, but the sum of elements in each column is <=1. To generate a random m by n matrix, we use x=randi([0,1],m,n), but what to add to force the sum of elements in each column be <=1...that is only single '1' element in each column.
0 Kommentare
Akzeptierte Antwort
Brendan Hamm
am 13 Mär. 2015
m = 5;
n = 6;
A = zeros(m,n);
for k = 1:n
c = randi([0,m]);
if c > 0
A(c,k) = 1;
end
end
0 Kommentare
Weitere Antworten (1)
Andrei Bobrov
am 13 Mär. 2015
x = zeros(5,7);
s = size(x);
x(sub2ind(s,randi([1 5],s(2),1),(1:s(2))')) = 1;
0 Kommentare
Siehe auch
Kategorien
Mehr zu Random Number Generation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!