Could anyone help me to solve the issue in the following code

I have run the following code:
one.m in one script
r = 2
c = 10
[x,v] = randfixedsum(r*c,1,0.35,0.01,0.09)
y = reshape(x,r,c)
sum(y(:))
------------------
In another script
function x = myfunction()
r = 2
c = 10
x = my_randfixedsum(r,c)
end
--------------------------------
In another script
function y = my_randfixedsum(r,c)
a = 0.01
b = 0.09
s = 0.35
n = r * c
m = 1
if n * a > s || n * b < s
error('Cannot create a sum of %f by adding together %d x %d numbers in the range %f to %f', s, r, c, a, b);
end
randomvec = randfixedsum(n, m, s, a, b)
y = reshape(randomvec, r, c)
end
when i run the code it works. but when i change the value of r=4 it results in Error using randfixedsum (line 46) Inequalities n*a <= s <= n*b and a < b must hold.
Error in one (line 6) [x,v] = randfixedsum(r*c,1,0.35,0.01,0.09). could you please help me on it. Could anyone help me to fix it.

2 Comments

@Prabha Kumaresan: why did you copy the code and help I gave in my answer ... yet accepted a different (useless) answer and did not even give a single word of thanks for me helping you?:
Do you imagine that it is okay to take my knowledge andefforts and use it without thanking or acknowledging my (volunteered) time helping you? Taking someone's idea without acknowledgement is called plagiarism.
@Stephen: Prabha has a hard time to use Matlab and this forum. Of course it would be better, if Prabha cares about the customs in the forum e.g. by posting 1 thread per problem, answering questions for clarifications and carefully accepting the answer, which solves the problem. But as long as this is not the case, I'm sure it is the best not to take it personally.

Sign in to comment.

 Accepted Answer

Torsten
Torsten on 26 Jan 2018
Edited: Torsten on 26 Jan 2018
If you choose 40 numbers from the interval [0.01:0.09], they will at least sum to 40*0.01=0.4. But you chose the sum to be 0.35. That's not possible.
Best wishes
Torsten.

7 Comments

sorry it was from the interval[0.001,0.009].Now could you tell me how it can be done.
Now could you tell me how it can be done.
What do you mean ? You did not modify your question.
Best wishes
Torsten.
This was the following code i am working on
r = 2
c = 10
[x,v] = randfixedsum(r*c,1,0.35,0.01,0.09)
y = reshape(x,r,c)
sum(y(:))
--------------------calling function------------
function [x,v] = randfixedsum(n,m,s,a,b)
% Check the arguments.
if (m~=round(m))|(n~=round(n))|(m<0)|(n<1)
error('n must be a whole number and m a non-negative integer.')
elseif (s<n*a)|(s>n*b)|(a>=b)
error('Inequalities n*a <= s <= n*b and a < b must hold.')
end
% Rescale to a unit cube: 0 <= x(i) <= 1
s = (s-n*a)/(b-a);
% Construct the transition probability table, t.
% t(i,j) will be utilized only in the region where j <= i + 1.
k = max(min(floor(s),n-1),0); % Must have 0 <= k <= n-1
s = max(min(s,k+1),k); % Must have k <= s <= k+1
s1 = s - [k:-1:k-n+1];% s1 & s2 will never be negative
s2 = [k+n:-1:k+1] - s;
w = zeros(n,n+1);
w(1,2) = realmax; % Scale for full 'double' range
t = zeros(n-1,n);
tiny = 2^(-1074); % The smallest positive matlab 'double' no.
for i = 2:n
tmp1 = w(i-1,2:i+1).*s1(1:i)/i;
tmp2 = w(i-1,1:i).*s2(n-i+1:n)/i;
w(i,2:i+1) = tmp1 + tmp2;
tmp3 = w(i,2:i+1) + tiny; % In case tmp1 & tmp2 are both 0,
tmp4 = (s2(n-i+1:n) > s1(1:i)); % then t is 0 on left & 1 on right
t(i-1,1:i) = (tmp2./tmp3).*tmp4 + (1-tmp1./tmp3).*(~tmp4);
end
% Derive the polytope volume v from the appropriate
% element in the bottom row of w.
v = n^(3/2)*(w(n,k+2)/realmax)*(b-a)^(n-1);
% Now compute the matrix x.
x = zeros(n,m);
if m == 0, return, end % If m is zero, quit with x = []
rt = rand(n-1,m); % For random selection of simplex type
rs = rand(n-1,m) ;% For random location within a simplex
s = repmat(s,1,m);
j = repmat(k+1,1,m); % For indexing in the t table
sm = zeros(1,m); pr = ones(1,m); % Start with sum zero & product 1
for i = n-1:-1:1 % Work backwards in the t table
e = (rt(n-i,:)<=t(i,j)); % Use rt to choose a transition
sx = rs(n-i,:).^(1/i); % Use rs to compute next simplex coord.
sm = sm + (1-sx).*pr.*s/(i+1); % Update sum
pr = sx.*pr; % Update product
x(n-i,:) = sm + pr.*e ;% Calculate x using simplex coords.
s = s - e; j = j - e; % Transition adjustment
end
x(n,:) = sm + pr.*s ;% Compute the last x
% Randomly permute the order in the columns of x and rescale.
rp = rand(n,m); % Use rp to carry out a matrix 'randperm'
[ig,p] = sort(rp); % The values placed in ig are ignored
x = (b-a)*x(p+repmat([0:n:n*(m-1)],n,1))+a; % Permute & rescale x
return
I have taken randfixedsum from matlab central could you please explain what happens in randfixedsum
A Word document "Theory_of_randfixedsum.doc" is included.
Best wishes
Torsten.
"If you choose 40 numbers from the interval [0.01:0.09], they will at least sum to 40*0.01=0.4. But you chose the sum to be 0.35. That's not possible."
@Prabha Kumaresan: this is also exactly what I told you in my answer (which you have not given any thanks or credit for, even though it solved your question and you have copied the code I gave you into five other questions).
I am sorry for not accepting your answer.could you tell me where I need to accept your answer.
@Prabha: You have accepted another answer in this thread: https://www.mathworks.com/matlabcentral/answers/378681-could-anyone-help-me-to-fix-the-issue. But to be exact: You do not need to accept another answer. I would have selected Stephen's answer, if this is my thread - but it is yours.
It is irritating, that you accept an answer as solution, which is not working reliably, and use the code of another solution in a following question. While you can accept one answer only (but change your decision later), you can vote for good answers also and the old "thank you" is useful in addition.
But discussions about accepting answers are irritating also. They have never been useful in this forum. Stephen, 3 votes from Walter, Guillaume and me are a statement. Other readers of this thread are not in the danger of choosing the wrong solution, because only your one works reliably.
Prabha, you have been asked several times about the way you participate in this forum, see e.g. https://www.mathworks.com/matlabcentral/answers/378681-could-anyone-help-me-to-fix-the-issue#comment_528707. If you ignore these reactions, your question will be ignored sooner or later also. Please take care to get more focused.

Sign in to comment.

More Answers (0)

Asked:

on 26 Jan 2018

Edited:

Jan
on 26 Jan 2018

Community Treasure Hunt

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

Start Hunting!