필터 지우기
필터 지우기

if i have a i*j matrix(with random numbers ), then how can i make the sum of first row = d(say d= 5).

조회 수: 4 (최근 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
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.

Aykut Satici
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
reshdev
reshdev 2014년 8월 19일
편집: reshdev 2014년 8월 19일
Hello Sir, thank you for your valuable response. Actually, i am new to matlab programming.So, i don't know much programming. can you please modify the above program such that all the elements in resultant matrix should be >=0.
thank you
Actually,the main problem is...............(if you can help me for this)
Please help me to write matlab code, I am trying to create 5*5 matrix containing random values having constraints....... (1) sum of elements of row 1(X1) = sum of elements of column 1 (X1) (Sum = X1<= 7)............. (2) sum of elements of row 2 =7 and sum of elements of column 2 = 0 .......... (3) sum of elements of row 3(X3) = sum elements of column 3(X3) (Sum X3 <= 7)............ (4) sum of elements of row 4 =0 and sum of elements of column 4 = 7 ................ (5) sum of elements of row 5(X5) = sum elements of column 5(X5) (X5<= 7)........
Rows act as sources nodes. columns act as destination nodes.
Actually, this matrix represents a transshipment problem which is sending 7 units of data from (starting node 2) to (termination node 4).above constraints show that data going into node 2 from all nodes is zero and going out to all nodes is 7 units. Similarly, data going into destination node 4 is 7 units and nothing comes out from this. Whereas for all the other nodes, total amount of data coming in = total amount of data coming out.
all the diagonal elements should be zero. like (1,1)...(2,2).....(5,5) and range for all elements of matrix = 0 to 7
This is what i am able to do so far....
no_sourcenode = input ('ENTER THE NUMBER OF source NODE :');
no_destiantion = input ('ENTER THE NUMBER OF destination node :');
chromosome = round(rand(no_sourcenode,no_destiantion));
chromosomes(1:(no_sourcenode+1):no_sourcenode*no_destiantion)= 0; (to make diagonal elements zero)
Thank you very much
Resh
Guillaume
Guillaume 2014년 8월 19일
What you're asking now is completely different from what you've asked originally. You should create a new question.

댓글을 달려면 로그인하십시오.


Guillaume
Guillaume 2014년 8월 19일
편집: Guillaume 2014년 8월 19일
You don't say what you want to happen in the other rows, so I would just generate a random matrix, divide it by the sum of the first row and multiply it by d.
M = rand(nrows, ncols);
M = M / sum(M(1, :)) * d;
  댓글 수: 1
reshdev
reshdev 2014년 8월 19일
ok great. Thank you again. Now, you can have a look at my main problem. i have mentioned constraints that represents different sums for different rows and columns. I have also written a small program at the end. After that, i was unable to do anything.
Can you please take a look at that one too. It will be highly appreciated.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by