MATLAB: How to randomize the color of two objects so that are not the same color

colorrandomization

Here is my code:
random = rand(1)
if random <(1/4)
colour1 = [200 200 0]
elseif random>=(1/4) && random<(2/4)
colour1 = [200 0 0]
elseif random>=(2/4) && random<(3/4)
colour1 = [0 200 0]
elseif random>=(3/4) && random<(4/4)
colour1 = [0 0 200]
end
The above code works great – when I run the program it randomizes the color of an object between 4 different colors. However, I need code that randomizes the colors of another object so that if colour1 = [200 000 000], it will be one of the three available colors. I essentially need code that says something like colour2 (does not)= colour1, randomize the other three colors.
Any help would be very, very appreciated. 🙂

Best Answer

colarray = [200 200 0; 200 0 0; 0 200 0; 0 0 200];
colidx1 = randi(4,1,1);
colour1 = colarray(colidx1,:);
colidx2 = randi(4,1,1);
while ismember(colidx2, colidx1)
colidx2 = randi(4,1,1);
end
color2 = colarray(colidx2,:);
colidx3 = randi(4,1,1);
while ismember(colidx3, [colidx1, colidx2])
.... and you should be able to finish from here