私はランダムに点在する1s
との行列を持っています:-1s
0s
%// create matrix of 1s and -1s
hypwayt = randn(10,5);
hypwayt(hypwayt > 0) = 1;
hypwayt(hypwayt < 0) = -1;
%// create numz random indices at which to insert 0s (pairs of indices may
%// repeat, so final number of inserted zeros may be < numz)
numz = 15;
a = 1;
b = 10;
r = round((b-a).*rand(numz,1) + a);
s = round((5-1).*rand(numz,1) + a);
for nx = 1:numz
hypwayt(r(nx),s(nx)) = 0
end
入力:
hypwayt =
-1 1 1 1 1
1 -1 1 1 1
1 -1 1 0 0
-1 1 0 -1 1
1 -1 0 0 0
-1 1 -1 -1 -1
1 1 0 1 -1
0 1 -1 1 -1
-1 0 1 1 0
1 -1 0 -1 -1
nonzero
次のようなものを生成するために、要素が列で繰り返される回数を数えたいと思います。
基本的な考え方は (@rayryeng 提供) です。列ごとに個別に、一意の数値にヒットするたびに、累積実行カウンターをインクリメントし始め、前の数値と同じ数値にヒットするたびにインクリメントします。新しい数字を打つとすぐに 1 にリセットされますが、0 を打った場合を除き、0 です。
期待される出力:
hypwayt_runs =
1 1 1 1 1
1 1 2 2 2
2 2 3 0 0
1 1 0 1 1
1 1 0 0 0
1 1 1 1 1
1 2 0 1 2
0 3 1 2 3
1 0 1 3 0
1 1 0 1 1
これを達成するための最もクリーンな方法は何ですか?