How can i change values in matrix in random fashion?

조회 수: 1 (최근 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 CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by