Clear Filters
Clear Filters

Could anyone help me to fix the issue.

3 views (last 30 days)
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 Comments
John D'Errico
John D'Errico on 26 Jan 2018
Edited: John D'Errico on 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.

Sign in to comment.

Accepted Answer

Birdman
Birdman on 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 Comments
Stephen23
Stephen23 on 25 Jan 2018
@Prabha Kumaresan: why are you wasting your time on this? I showed you a working solution.
Jan
Jan on 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.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 24 Jan 2018
Edited: Stephen23 on 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 Comments
Prabha Kumaresan
Prabha Kumaresan on 26 Jan 2018
ok.Whos answer do i need to unaccept so that i can accept stephen answer
Rik
Rik on 26 Jan 2018
The only other answer in thread: the one from Birdman, which you will find between this answer and your question.

Sign in to comment.

Categories

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!