Effacer les filtres
Effacer les filtres

matrix of zeros and ones within number of zeros in each rows in-between a limit

2 vues (au cours des 30 derniers jours)
want to have a 30x40 matrix of zeros and ones; however in each row total number of 'ones' must be within 10 to 20.

Réponse acceptée

Thorsten
Thorsten le 9 Oct 2015
Modifié(e) : Thorsten le 9 Oct 2015
A = zeros(40,30);
Omin = 10; Omax = 20;
for i = 1:size(A, 2)
r = randperm(40);
ind = r(1:(Omin-1) + randi(Omax - Omin +1)) + (i-1)*40;
A(ind) = 1;
end
A = A';
imshow(A)
sum(A,2)
  2 commentaires
Biswanath Mahanty
Biswanath Mahanty le 10 Oct 2015
Thank you for your reply, I got the approach and have the solution
A=zeros(30,40);
for i = 1:size(A,1) %%in each itr a new row to be modified
r=randperm(40); %%randomizing column index
no1s=randi([10,15]); %%no of "0" to be replace with "1" in that row
index=r(1:no1s); %%getting selected column index to be modified "1"
A(i,index)=1; %%column index been converted to "1"
end
Image Analyst
Image Analyst le 10 Oct 2015
Go ahead and mark it as "Accepted" then, so he gets reputation points.

Connectez-vous pour commenter.

Plus de réponses (1)

John D'Errico
John D'Errico le 9 Oct 2015
So generate a matrix with random ones and zeros. Discard any rows that fail this test, and regenerate just those rows. WTP?
Since you have not said what the distribution of the number of ones is, I cannot offer a complete solution, but it is simple in any case. If you will have 17 ones in a row, then generate a row vector with 17 ones, and 23 zeros. Then randomly permute the elements.

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by