# I have to declare 6 variables with the constraint 0<=a1<b1<c​1<a2<b2<c2​<=255. How to do it? please help.

2 views (last 30 days)
Shramana GS on 17 Feb 2016
Edited: Shramana GS on 23 Feb 2016

Azzi Abdelmalek on 17 Feb 2016
What do you mean by < I have to declare 6 variables> ?
Walter Roberson on 17 Feb 2016
Is this a question about using the Symbolic Toolbox and adding "assume" conditions?

Azzi Abdelmalek on 17 Feb 2016
Edited: Azzi Abdelmalek on 17 Feb 2016
You can check using this test
test=0<=a1 & a1<b1 & b1<c1 & c1<a2 & a2<b2 & b2<c2 & c2<=255

Shramana GS on 21 Feb 2016
Thanks for your reply. But the solution didnt work. I need to initialize these 6 variables randomly satisfying the constraint 0<=a1<b1<c1<a2<b2<c2<=255. Plz help.
Walter Roberson on 21 Feb 2016
Would 0<a1<b1<c1<a2<b2<c2<255 be okay? The equality conditions at the end are a bit tricky to handle while maintaining uniform random distribution.
val6 = {};
while length(val6) < 6
val6 = num2cell( unique(rand(1,6)*255) );
end
[a1, b1, c1, a2, b2, c2] = val6{:};
The above will never equal exactly 0, or exactly 255, but can take on nearly any representable value in-between.
The equality constraints are not bad to handle if you are looking for integer values instead of floating point:
val6 = {};
while length(va6) < 6
val6 = num2cell( unique(randi([0 255], 1, 6)) );
end
[a1, b1, c1, a2, b2, c2] = val6{:};
Shramana GS on 23 Feb 2016
Thanks for the suggestion. It worked. :) .

Matt J on 17 Feb 2016
If you mean you just want to generate 6 variables satisfying those bounds, this is one of many things you can do.
x=num2cell(cumsum(rand(1,6))*255/6);
[a1,b1,c1,a2,b2,c2]=deal(x{:});

Jos (10584) on 17 Feb 2016
Note that this will not produce random values for the variables:
x = cumsum(rand(6,10000))*255/6) ;
histogram(x(:),0:10:255)
Matt J on 21 Feb 2016
Jos, I think you mean that the variables will be random, but not uniformly distributed. That's fine. The OP made no such requirement, or really any specific requirements at all.
However, if uniformity were required, I would have proposed using RANDFIXEDSUM
z=randfixedsum(6,1,rand,0,1);
x=num2cell( cumsum(z)*255 );
[a1,b1,c1,a2,b2,c2]=deal(x{:});