How can i change values in matrix in random fashion?

3 vues (au cours des 30 derniers jours)
DEEPAK Chekuri
DEEPAK Chekuri le 4 Déc 2020
Commenté : Cris LaPierre le 4 Déc 2020
i have this matrix
p=[1 -1 -1 -1 -1 1 -1;
1 1 1 -1 -1 1 -1]
how can i randomly change the values at any two positions in this matrix.
note : random numbers are to be either 1 or -1

Réponse acceptée

Cris LaPierre
Cris LaPierre le 4 Déc 2020
I'd use numel, and randi to identify 2 random points, and then, using linear indexing, multipy those 2 points by -1.
  2 commentaires
Steven Lord
Steven Lord le 4 Déc 2020
You probably want randperm not randi so you don't swap a point with itself. You don't want to reverse the polarity of the neutron flow if it's already had its polarity reversed; that would confuse the polarity!
Cris LaPierre
Cris LaPierre le 4 Déc 2020
Good catch.

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by