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

1 vue (au cours des 30 derniers jours)
I have to declare 6 variables with the constraint 0<=a1<b1<c1<a2<b2<c2<=255. How to do it? please help. If possible please provide the code. Thanks in advance.
  2 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 17 Fév 2016
What do you mean by < I have to declare 6 variables> ?
Walter Roberson
Walter Roberson le 17 Fév 2016
Is this a question about using the Symbolic Toolbox and adding "assume" conditions?

Connectez-vous pour commenter.

Réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 17 Fév 2016
Modifié(e) : Azzi Abdelmalek le 17 Fév 2016
You can check using this test
test=0<=a1 & a1<b1 & b1<c1 & c1<a2 & a2<b2 & b2<c2 & c2<=255
  3 commentaires
Walter Roberson
Walter Roberson le 21 Fév 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
Shramana GS le 23 Fév 2016
Modifié(e) : Shramana GS le 23 Fév 2016
Thanks for the suggestion. It worked. :) .

Connectez-vous pour commenter.


Matt J
Matt J le 17 Fév 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{:});
  2 commentaires
Jos (10584)
Jos (10584) le 17 Fév 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
Matt J le 21 Fév 2016
Modifié(e) : Matt J le 21 Fév 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{:});

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by