How can I exclude an index from generating a random number?

8 vues (au cours des 30 derniers jours)
Anne van Voorthuijsen
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 :)

Réponse acceptée

madhan ravi
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
Anne van Voorthuijsen
Anne van Voorthuijsen le 3 Juin 2020
Thank you for your answer!
Hoe could I achieve a matrix with only a one at one position in each row? But still not on the diagonal. So for example:
[0, 0, 1; 1, 0, 0; 0, 1, 0]
madhan ravi
madhan ravi le 3 Juin 2020
Well I believe I answered your original question.

Connectez-vous pour commenter.

Plus de réponses (0)

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!

Translated by