Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want the code to make this. I am not able to make this.

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.


ss(1)=0;
for q=2:number+1
ss(q)=ss(q-1)+facility_units(q-1);
end
ss

dist=zeros(tu,tu);
for i=1:number
for j=1:number
if di(i,j)~=0 && i~=j && i<j>
for l=ss(j-1)+1:ss(j)
for m=ss(j)+1:ss(j+1)
dist(l,m)=di(i,j);
end
end

for l=ss(j)+1:ss(j+1)
for m=ss(j-1)+1:ss(j)
dist(l,m)=di(i,j);
end
end
end
end
end

dist
Posted
Updated 27-Mar-15 7:08am
v2
Comments
shweta89 27-Mar-15 13:08pm    
not getting the exact output. please tell me where I am wrong.
ss(1)=0;
for q=2:number+1
ss(q)=ss(q-1)+facility_units(q-1);
end
ss

dist=zeros(tu,tu);
for i=1:number
for j=1:number
if di(i,j)~=0 && i~=j && i

1 solution

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
shweta89 27-Mar-15 13:03pm    
I have already make a code for this but not getting the exact output. I tried it number of times. can you please check it and tell me where I am doing wrong thing. the code is in MATLAB. but the logic of C and MATLAB is same to make this.

number=4
facility_units=[4 3 2 1]
tu=10
d=7;
t = triu(d.*rand(number),1) % The upper trianglar random values
M = t+t.'
di=zeros(number,number); % initialize with zeros
for i=1:number
for j=1:number
if M(i,j)>0 && M(i,j)<0.49 %if values lies between 0 to 0.49 than set it to 1
M(i,j)=1;
end
di(i,j)=round(M(i,j)); % round off the value to get distance matrix
end
end
di

tt=facility_units(1);
sss(1)=tt;
for q=2:number
sss(q)=sss(q-1)+facility_units(q);
end
sss

ss(1)=0;
for q=2:number+1
ss(q)=ss(q-1)+facility_units(q-1);
end
ss

dist=zeros(tu,tu);
for i=1:number
for j=1:number
if di(i,j)~=0 && i~=j && i
OriginalGriff 27-Mar-15 13:10pm    
So use the debugger and work out exactly what is going on!
This is a skill: and the only way to get good at it is to do it. It's a lot easier to start with simple code like this and get good at debugging than it is to try and do it with "real world" complicated code.
shweta89 27-Mar-15 13:26pm    
i tried it and will try again too.
i want the output that looks like symmetric matrix. by getting value from one matrix and frequency depend on vector.
is there any other method to make the block matrix. blockdiagonal matrix is easy to make in matlab. but not getting any function to make blockmatrix.

well thanks for your help. I am new in this coding field so i need some help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900