필터 지우기
필터 지우기

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 CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by