if i have a i*j matrix(with random numbers ), then how can i make the sum of first row = d(say d= 5).
2 次查看(过去 30 天)
显示 更早的评论
HELLO,
I want to make random number matrix having the sum of first row elements equal to d (a positive number). I tried to use Roger Stafford's randomfixedsum for first row but was not successful.
thank you
Resh
回答(3 个)
Orion
2014-8-19
编辑:Orion
2014-8-19
You can do this: http://www.mathworks.com/matlabcentral/newsreader/view_thread/318632 And you don't need a value constraint in the first line.
0 个评论
Aykut Satici
2014-8-19
I understand that you would like to get a matrix A with m rows and n columns, and your constraint is that the first row has to sum to d
This means you can arbitrarily select (n-1) of these numbers such that their sum is less than d and then assign the last one such that the whole sum is d.
As a result, one way of doing this would be to generate (n-1) random numbers all of which is smaller or equal to d/(n-1) and then select the last one as d minus the sum of the previous (n-1). See the code below:
m = 4; % number of rows
n = 7; % number of columns
d = 5; % desired sum of the first column
a = 10/17; % lower bound on the elements of the first row of A
% Generate the matrix
A = zeros(m,n); % Create a matrix of zeros
% of dimension m-by-n
A(2:m,:) = rand(m-1,n); % Populate the elements of the
% matrix through rows 2 to m
% with random numbers
A(1,1:n-1) = a + (d/(n-1)-a)*rand(1,n-1); % Generate (n-1) random numbers
% between a and d/(n-1)
% (see "help rand")
A(1,n) = d - sum(A(1,1:n-1));
2 个评论
Guillaume
2014-8-19
What you're asking now is completely different from what you've asked originally. You should create a new question.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!