How do I solve this problem? I want to make code to solve this problem. I made the matrix D using triu function in matlab and than D=t+t.'

1 view (last 30 days)
I have one vector and one matrix..
Facility_units=[4 2 3 1]
total=4+2+3+1=10
matrix D=
[0 4 2 4 ;
4 0 1 3 ;
2 1 0 2 ;
4 3 2 0]
D is symmetric matrix in which diagonal element is 0 and upper triangle is similar to lower triangle.
I want the output in this form e.g.
output= 10*10 matrix i.e. total*total
[
0 0 0 0 4 4 2 2 2 4 ;
0 0 0 0 4 4 2 2 2 4 ;
0 0 0 0 4 4 2 2 2 4 ;
0 0 0 0 4 4 2 2 2 4 ;
4 4 4 4 0 0 1 1 1 3 ;
4 4 4 4 0 0 1 1 1 3 ;
2 2 2 2 1 1 0 0 0 2 ;
2 2 2 2 1 1 0 0 0 2 ;
2 2 2 2 1 1 0 0 0 2 ;
4 4 4 4 3 3 2 2 2 0 ]
output matrix is 10-by-10 in which elements are repeated according to vector Facility_units.
1st facility having 4 units, 2nd has 2 units, 3rd has 3 units and 4th has 1 unit.
D is the matrix shows the distance between each facility. e.g. distance between facility 1 and 2 is 4,
between 1 and 3 is 2 and between 1 and 4 is 4. according to that matrix D (i.e. distance matrix) is made.
Now i want the output that shows the distance between each unit of the facility with any other units.
e.g. units of same facility having distance 0. 1st facility have 4 units that is why in output matrix 1:4 row
by 1:4 column is 0.
than distance of units of facility 2 with facility 1 is 2.

Accepted Answer

Roger Stafford
Roger Stafford on 28 Mar 2015
'Facility_units' must be a row vector of non-negative integers in the following:
c = cumsum(Facility_units);
ix = cumsum(accumarray([1,c(1:end-1)+1].',1,[c(end)+1,1]));
output = D(ix(1:end-1),ix(1:end-1));

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!