I have to declare 6 variables with the constraint 0<=a1<b1<c1<a2<b2<c2<=255. How to do it? please help.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
Walter Roberson
le 17 Fév 2016
Is this a question about using the Symbolic Toolbox and adding "assume" conditions?
Réponses (2)
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
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{:};
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)
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
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.
z=randfixedsum(6,1,rand,0,1);
x=num2cell( cumsum(z)*255 );
[a1,b1,c1,a2,b2,c2]=deal(x{:});
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!