Creating a data matrix that self generates.

I want to create a 10x10 matrix using 0(x99) & 1(x1) that will randomly regenerate 'n' times. I also want to count when 1 changes position within the matrix.
For example:
Original: 1st Gen: 2nd Gen:
1 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Counter = 0 Counter = 1 Counter = 2
Can anyone think of a way to do this?
Thanks,
Joe

回答 (1 件)

Stijn Haenen
Stijn Haenen 2020 年 5 月 18 日

1 投票

somthing like this:
count=0;
m=zeros(100,1);
rand_int=datasample(1:100,1);
m(rand_int)=1;
old_matrix=reshape(m,[10 10]);
for i=1:10
m=zeros(100,1);
rand_int=datasample(1:100,1);
m(rand_int)=1;
new_matrix=reshape(m,[10 10]);
if sum(abs(old_matrix-new_matrix),'All')>0
count=count+1;
end
old_matrix=new_matrix
end

1 件のコメント

Joseph Lee
Joseph Lee 2020 年 5 月 18 日
Yes thats exactly what I asked for thanks.
Do you know how I could modify this script so that the 1 can only move one digit from the position it is currently at per generated matrix?
Also is there a way of setting an alarm if the 1 doesn't move from its position for x number of generated matricies and resetting said alarm when its position moves?
Many thanks,
Joe

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2020 年 5 月 18 日

コメント済み:

2020 年 5 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by