Randomization... if not this then that

Here is my code:
random = rand(1)
if random <(1/4)
tri1 = downtri
b1 = vertbaseq1
elseif random>=(1/4) && random<(2/4)
tri1 = uptri
b1 = vertbaseq1
elseif random>=(2/4) && random<(3/4)
tri1 = righttri
b1 = horzbaseq1
elseif random>=(3/4) && random<(4/4)
tri1 = lefttri
b1 = horzbaseq1
end
if random <(1/4)
tri2 = downtri
b2 = vertbaseq2
elseif random>=(1/4) && random<(2/4)
tri2 = uptri
b2 = vertbaseq2
elseif random>=(2/4) && random<(3/4)
tri2 = righttri
b2 = horzbaseq2
elseif random>=(3/4) && random<(4/4)
tri2 = lefttri
b2 = horzbaseq2
end
...
So what this code basically does is randomize between four different triangles at four different locations. The problem: it randomizes the same triangle to each location. I need the some code that says something like if triangle 1 (tri1) is an up triangle (uptri) then triangle 2 will be any triangle that isn't an up triangle.
any help would be greatly, greatly! appreciated, Brett

 Respuesta aceptada

Image Analyst
Image Analyst el 25 de Nov. de 2012
After your first if, have this instead of the second if:
if tri1 == uptri
% Triangle 2 can't be an uptriangle.
random = rand(1);
if random <(1/3)
tri2 = downtri
b2 = vertbaseq2
elseif random>=(1/3) && random<(2/3)
tri2 = righttri
b2 = horzbaseq2
else
tri2 = lefttri
b2 = horzbaseq2
end
else
% Triangle 2 can be anything.
if random <(1/4)
tri2 = downtri
b2 = vertbaseq2
elseif random>=(1/4) && random<(2/4)
tri2 = uptri
b2 = vertbaseq2
elseif random>=(2/4) && random<(3/4)
tri2 = righttri
b2 = horzbaseq2
elseif random>=(3/4) && random<(4/4)
tri2 = lefttri
b2 = horzbaseq2
end
end

4 comentarios

Brett
Brett el 26 de Nov. de 2012
Editada: Brett el 26 de Nov. de 2012
Sorry, I didn't explain the problem well - this does exactly what I said, so thank you. But what I need is that triangle 2 be any triangle that triangle 1 is not (if tri1 = uptri,then tri2 = [downtri, righttri or lefttri] or if tri1 = righttri, tri2 = [downtri, uptri or lefttri] etc). Is there a way to make it so tri2 doesn't equal the value of tri1?
Image Analyst
Image Analyst el 26 de Nov. de 2012
Editada: Image Analyst el 26 de Nov. de 2012
Try randperm then to make sure they are different:
random = randperm(4)
random1 = random(1)
random2 = random(2)
% random1 and random2 are guaranteed to be different
% First assign triangle1
if random1 == 1
tri1 = downtri
b1 = vertbaseq1
elseif random1 == 2
tri1 = uptri
b1 = vertbaseq1
elseif random1 == 3
tri1 = righttri
b1 = horzbaseq1
else
tri1 = lefttri
b1 = horzbaseq1
end
% Next assign triangle2
% Remember random1 and random2 are different
% so triangle2 will be different than triangle1.
if random2 == 1
tri2 = downtri
b2 = vertbaseq2
elseif random2 == 2
tri2 = uptri
b2 = vertbaseq2
elseif random2 == 3
tri2 = righttri
b2 = horzbaseq2
else
tri2 = lefttri
b2 = horzbaseq2
end
Brett
Brett el 27 de Nov. de 2012
This works perfectly! I can't thank you enough!
Image Analyst
Image Analyst el 27 de Nov. de 2012
You're welcome. Not bad for me just writing it and not even being able to run it or test it!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Random Number Generation en Centro de ayuda y File Exchange.

Preguntada:

el 25 de Nov. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by