フィルターのクリア

getting a vector with random numbers but with new criteria

3 ビュー (過去 30 日間)
itay
itay 2015 年 1 月 9 日
コメント済み: itay 2015 年 1 月 9 日
i need to get an 80 cells vector with random numbers that will be between 1 to 8.
each number, x for example, need to be different from x+1 and x-1, and also different from x+2 and x-2.
to make it clear:
what i need is like: 4-1-5-3-2-6-4-2-3-5-...
and what i have now is: 3-5-1-5-7-4-5-3-4-3-...
is it possible in matlab?
thanks.
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 1 月 9 日
There is no a general solution. You have to precise what you want
itay
itay 2015 年 1 月 9 日
what do you mean not general?
i need "n" random numbers that are betwwen the range "k to z" that the numbers in places "x+1","x-1", and "x+2", "x-2" are different then the number that in place "x"..
that will help me on making a task where i can run few random pictures and every few pictures i have a repeat on the last one showed (like: a - b - c - d - d - a - e - e - f - g - e - a - a - d - ...)

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

回答 (1 件)

Roger Stafford
Roger Stafford 2015 年 1 月 9 日
編集済み: Roger Stafford 2015 年 1 月 9 日
You want n random integers, each ranging from k to z, such that each differs from the two previous integers. Call the vector of integers V and do this:
V = zeros(1,n);
V(1) = randi([k,z]);
d = setdiff(k:z,V(1));
V(2) = d(randi(z-k));
for m = 3:n
d = setdiff(k:z,[V(m-2),V(m-1)]);
V(m) = d(randi(z-k-1));
end

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by