How can i change values in matrix in random fashion?

3 ビュー (過去 30 日間)
DEEPAK Chekuri
DEEPAK Chekuri 2020 年 12 月 4 日
コメント済み: Cris LaPierre 2020 年 12 月 4 日
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!
Cris LaPierre
Cris LaPierre 2020 年 12 月 4 日
Good catch.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by