A = [8 1 5; 1 4 2; 7 5 2];
Max = 5
B = randi(Max);
現在、乱数を生成するコードの一部があります。数値のリストから乱数を生成しようとしています。この場合は、最初の行にリストされている数値です ( 8 1 5
)。
使用する代わりにrandi
、最初の行にリストされている数字の 1 つをランダムに生成し、Max
基準を満たす別の関数はありますか?
あなたが指定したことから、私は次のことを提案します:
A = [8 1 5; 1 4 2; 7 5 2];
% get a random number from row 1
index = randperm(length(A(1,:)));
number = A(1,index(1))
% get a randome number from row 1 that does not exceed Max
max = 5;
condition = find(A(1,:) <= max);
index = randperm(length(A(1,condition)));
number = A( 1, condition(index(1)))
これがいくつかのアイデアを与えることを願って、
最も簡単な方法は、使用することarrayfun
です:
B = arrayfun(@randi, A(1,:))