Could anyone help me to fix the issue.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
If A=rand(5,10) how to have the values by fixing the minimum value to be 0.01 and maximum value to be 0.09 and by adding all the random values together it should result in 0.35. For example I have taken the minimum value and maximum value to be 0.01 and 0.09. And the addition of all the values should result in 0.35.Could anyone help me how to implement it.
2 commentaires
John D'Errico
le 26 Jan 2018
Modifié(e) : John D'Errico
le 26 Jan 2018
Why do you feel it necessary to ask the same question always at least TWICE, and often 3 or 4 times? You got an answer several times, with several ways to do it.
You have now asked something over 170 basic questions, and as has been pointed out, do not seem to be learning the basics of MATLAB yet. Surely it it time to read the documentation, rather than relying on Answers as your lazy manual? In this case, had you simply done a search on answers, you would have found the same question asked and answered repeatedly.
Stephen23
le 26 Jan 2018
Réponse acceptée
Birdman
le 24 Jan 2018
One way:
a=0.01;b=0.09;N=10;
while true
A=a+(b-a).*rand(N,1)
if ismembertol(sum(A(:)),0.35,1e-2)
break;
else
end
end
13 commentaires
Stephen23
le 25 Jan 2018
@Prabha Kumaresan: why are you wasting your time on this? I showed you a working solution.
Jan
le 25 Jan 2018
@Prabha: Birdman's code produced random numbers until the sum has the wanted value accidentally. You see that this takes a long time for 16 numbers. Note that this solution accepts a sum, which is not exactly 0.35, but uses a tolerance of 0.01 . For a reliable working code see Stephen's answer.
Plus de réponses (1)
Stephen23
le 24 Jan 2018
Modifié(e) : Stephen23
le 25 Jan 2018
"If A=rand(5,10) how to have the values by fixing the minimum value to be 0.01 and maximum value to be 0.09 and by adding all the random values together it should result in 0.35"
This is impossible: matrix A has size 5x10 with 5*10=50 elements, which means that the minimum possible sum of all of its values will be 0.01*50=0.5, which is greater than the requested sum of 0.35.
Once you select values or a matrix size that are actually possible, then the simplest solution is to download and use Roger Stafford's excellent randfixedsum:
Here is a complete working example for a more realistic matrix size of 2x10:
>> r = 2;
>> c = 10;
>> [x,v] = randfixedsum(r*c,1,0.35,0.01,0.09);
>> y = reshape(x,r,c)
y =
0.018153 0.011913 0.025947 0.020434 0.010300 0.027032 0.017784 0.018632 0.025201 0.015418
0.014428 0.013693 0.012165 0.012403 0.026608 0.015631 0.013894 0.012895 0.014529 0.022941
>> sum(y(:))
ans = 0.35000
8 commentaires
Rik
le 26 Jan 2018
The only other answer in thread: the one from Birdman, which you will find between this answer and your question.
Voir également
Catégories
En savoir plus sur Variables 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!