How can I exclude an index from generating a random number?
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Anne van Voorthuijsen
le 3 Juin 2020
Commenté : madhan ravi
le 3 Juin 2020
Hello,
I want to generate a matrix that has in every row one element set to one. However, the numbers on the diagonals should not be one.
This is the code I have now. However, j can not be equal to i, but I don't know how to exclude i from generating j.
W = zeros(N,N);
for i = 1:N
j = randi(N);
W(i, j) = 1;
end
I hope someone can help! Thanks in advance :)
0 commentaires
Réponse acceptée
madhan ravi
le 3 Juin 2020
Modifié(e) : madhan ravi
le 3 Juin 2020
W = zeros(N,N); for ii = 1:N
for jj = 1:N
if ii ~= jj
Hj = randi(N);
W(ii, jj) = 1;
end
end
end
3 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices 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!