我该如何制作这种类型的矩阵? [英] How do I make this type matrix?

查看:61
本文介绍了我该如何制作这种类型的矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望代码能够做到这一点。我无法做到这一点。



我有一个向量和一个矩阵..



Facility_units = [4 2 3 1]

总计= 4 + 2 + 3 + 1 = 10



矩阵D =

[0 4 2 4;

4 0 1 3;

2 1 0 2;

4 3 2 0]

D是对称矩阵,其中对角线元素为0,上三角形类似于下三角形。



我想要这种形式的输出,例如



输出= 10 * 10矩阵即总计*总额



[

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]



输出矩阵是10乘10,其中元素根据向量Facility_units重复。

第一个设施有4个单位,第二个有2个单位,第三个有3个单位,第四个有1个单位。

D是表示每个设施之间距离的矩阵。例如根据矩阵D(即距离矩阵),设施1和2之间的距离为4,1到3之间为2,1到4之间为4.



现在我想要输出显示设施的每个单元与任何其他单位之间的距离。例如具有距离0的相同设施的单位。第一设施有4个单位,这就是为什么在输出矩阵1:4行乘以1:4列为0.

比设施2的设备1的单位的距离是2.





ss(1)= 0;

for q = 2:number + 1

ss(q)= ss(q-1)+ facility_units(q-1);

结束

ss



dist =零(tu,tu);

表示i = 1:数字

表示j = 1:数字

如果di(i,j)〜= 0&&我〜= j&& i< j>

代表l = ss(j-1)+1:ss(j)

代表m = ss(j)+1:ss(j + 1)

dist(l,m)= di(i,j);

结束

结束



代表l = ss(j)+1:ss(j + 1)

代表m = ss(j-1)+1:ss(j)

dist(l,m)= di(i,j);

结束

结束

结束
结束

结束



dist

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

推荐答案

我们不要做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!
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!


这篇关于我该如何制作这种类型的矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆