RANDOM FLIPPING FROM 1 TO -1

3 ビュー (過去 30 日間)
Offroad Jeep
Offroad Jeep 2016 年 5 月 24 日
編集済み: Walter Roberson 2016 年 5 月 25 日
Hi to all the users, I have tried a code in which numbers are changed from 1 to -1 turn by turn. But I need some condition.
  1. All elements are 1
  2. A random element of matrix is selected and changed from 1 to -1.
  3. The process goes till all the elements are changed from 1 to -1 randomly.
  4. Once the change happens it should be fixed. ie once 1 is changed to -1 it should remain fixed.
Hope i have cleared my idea.....
Regards
clc
clear all
format compact
nrows = 3
A = ones(nrows)
E_A = sum(sum(A))
n = numel(A);
for k=1:n
A(k) = -1
E_B = sum(sum(A))
end

採用された回答

James Tursa
James Tursa 2016 年 5 月 24 日
編集済み: James Tursa 2016 年 5 月 24 日
Make this change:
:
p = randperm(n); % <-- Add this line just before for-loop
for k=1:n
A(p(k)) = -1; % <-- change the index from k to p(k)
:
  1 件のコメント
Offroad Jeep
Offroad Jeep 2016 年 5 月 24 日
Thanks..........

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

その他の回答 (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