program to create matrix obeying constraints

Hello all,
i have following code to create n*n matrix(say n=5 this time) which is following certain constraints.
function selectingset
% n = input ('Enter Num_Nodes :');
% inputData=input ('Enter inputdata :');
n=5;
inputData=[5 1 2];
for z = 1:n
f = inputData(1,1); % f= Demand
S = inputData(1,2); % S= Source node
T = inputData(1,3); % T= Termination node
[idx,p] = sort(rand(n-1,f));
M = accumarray([reshape(p+(p>=T),[],1),repmat([1:S-1,S+1:n]',f,1)],1,[n,n]);
M(1:n+1:n^2) = 0;
end
disp(M);
end
So, my o/p from following code is---
0 0 2 3 0
0 0 0 0 0
0 0 0 0 3
0 4 1 0 0
0 1 0 2 0
Note that as in given code
input S=1, so column 1 is zero and
As T=2, so row 2 is zero.
As f= 5, so sum of row S(i.e. S=1)=Sum of column T(i.e. T=2)= 5
and i am making diagonal elements zero.
all the remaining rows and columns, sum of row(i)=sum of column(i)
Now, How can i modify code such that row(1,3) will always come zero in output but output still follows previous mentioned constraints.

4 Kommentare

Matt J
Matt J am 20 Sep. 2014
Your constraints can never be satisfied when S=T. Will that never be the case?
Matt J
Matt J am 20 Sep. 2014
Bearbeitet: Matt J am 20 Sep. 2014
Also, what does "row(1,3)" mean? Is it the entire 1st row and the entire 3rd row? Or did you really mean, position (1,3) in the matrix?
How does "row(1,3)" generalize to other n?
Matt J
Matt J am 20 Sep. 2014
Do all the matrix entries have to be integers?
reshdev
reshdev am 20 Sep. 2014
Hello,
S will never be equal to T.
row(1,3) means third element in first row.
all the matrix elements should be integers between 0 to f.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt J
Matt J am 20 Sep. 2014
Bearbeitet: Matt J am 20 Sep. 2014

0 Stimmen

If you have the latext version of the Optimization Toolbox, you can use intlinprog to solve for the unknown elements of the matrix. You can choose any objective function that you want. See this recent thread for a very similar problem related to constraining rows and columns of matrices:
This assumes that you require the entries to be integer-valued and that a solution exists, see also My Comments. If they don't have to be integer-valued, you could just use linprog .

2 Kommentare

reshdev
reshdev am 20 Sep. 2014
Hello,
S will never be equal to T.
row(1,3) means third element in first row.
all the matrix elements should be integers between 0 to f.
Matt J
Matt J am 20 Sep. 2014
OK. Then I guess I've answered your question.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 20 Sep. 2014

Kommentiert:

am 20 Sep. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by