How can i change values in matrix in random fashion?

2 次查看(过去 30 天)
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

采纳的回答

Cris LaPierre
Cris LaPierre 2020-12-4
I'd use numel, and randi to identify 2 random points, and then, using linear indexing, multipy those 2 points by -1.
  2 个评论
Steven Lord
Steven Lord 2020-12-4
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by