Could anyone help me to fix the issue.

3 次查看(过去 30 天)
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 个评论
John D'Errico
John D'Errico 2018-1-26
编辑:John D'Errico 2018-1-26
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.

请先登录,再进行评论。

采纳的回答

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

请先登录,再进行评论。

更多回答(1 个)

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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by