Please help me to modify the following program

1 Ansicht (letzte 30 Tage)
reshdev
reshdev am 20 Aug. 2014
Beantwortet: Image Analyst am 21 Aug. 2014
okay, thanks to Roger.i have got program with following constraints.
A should be a randomly created 5 x 5 matrix whose elements are all integers ranging from 0 to 7 in such a way that
1) row 1 and column 1 have equal sums,
2) row 2 has a sum of 7 and column 2 has a sum of 0,()
3) row 3 and column 3 have equal sums,
4) row 4 has a sum of 0 and column 4 has a sum of 7,
5) row 5 and column 5 have equal sums,
6) its diagonal elements are all zeros, and
7) all column and row sums are 7 or less,
A = zeros(4);
for k = 1:7
p = randperm(4); % <-- This is the source of randomness
for ix = 1:4
A(p(ix),ix) = A(p(ix),ix) + 1;
end
end
A = [A(:,1),zeros(4,1),A(:,2:4)]; % Insert a column of four zeros
A = [A(1:3,:);zeros(1,5);A(4,:)]; % Insert a row of five zeros
A(1,1) = 0; A(3,3) = 0; A(5,5) = 0; % Set non-zero diagonal elements to 0
now, i want that this kind of program should take input in command window as-
1. Enter n = (in above case, we had n=5, we got 5*5 matrix)
2. Enter d= ()(in upper case, we have d=7)
3. enter S(source node)= (in upper case, we have S=2 and so, constraint should be row 2(S) has a sum of 7(d) and column 2(S) has a sum of 0 )
4. enter T(termination node)= (in upper case, we have T=4 and constraint- row 4(T) has a sum of 0 and column 4(T) has a sum of 7(d))
5.Each matrix element should be integer in range o to d.
6. All the other corresponding rows and column should should be equal.e.g. row 1 and column 1 have equal sums, row 5 and column 5 have equal sums etc.
7. diagonal elements zero.
8. all column and row sums are d or less Thank You

Akzeptierte Antwort

Roger Stafford
Roger Stafford am 21 Aug. 2014
[~,p] = sort(rand(n-1,d));
A = accumarray([reshape(p+(p>=T),[],1),repmat([1:S-1,S+1:n]',d,1)],1,[n,n]);
A(1:n+1:n^2) = 0;

Weitere Antworten (1)

Image Analyst
Image Analyst am 21 Aug. 2014
You can get all of that user input in one dialog box. Just put your prompts into inputdlg(). See the examples in the help and let us know if you can't figure out how to ask your questions/prompts or how to get the output from inputdlg().

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by