创建带有零元素矩阵的矩阵

2次观看(最近30天)
Ali Tawfik
Ali Tawfik 2020年1月24日
回答: 辛达 2020年1月29日
我有一个矩阵
a = [1 2 3 4; 5 6 7 8; 9 10 11 12;13 14 15 16];
因此,我只想获得另一个矩阵,但是有一些索引(或元素),其余的就是零
所以我需要输出矩阵为
o = [1 2 0 0; 2 1 0 0; 0 0 6 8; 0 0 8 16]
So can anyone advise me with the best way ?
谢谢,
2条评论
Ali Tawfik
Ali Tawfik 2020年1月27日
嗨,辛达,
感谢你的及时回复。
好吧,我试图向您解释更多,
I had already a matrix, and I wanna create a new matrix based on matrix obtained earlier, then assign zeros in the matrix with some elements from the first matrix,
For example:
x = [1 2 3 4 5 6;
7 8 9 10 11 12;
13 14 15 16 17 18;
19 20 21 22 23 24;
25 26 27 28 29 30;
31 32 33 34 35 36]
然后,我想创建Y作为以下内容:
y = [x(1,1)x(1,4)0 0;
x(1,4)x(4,4)0 0;
0 0 x(3,3)x(6,3);
0 0 x(6,3)x(6,6)]
我创建了0(4)所以我有新矩阵,我什么estion is how to assign some elements to the new matrix to create Y ???
谢谢,

登录发表评论。

答案(2)

斯宾塞Chen
斯宾塞Chen 2020年1月24日
您可以创建一个相同大小的新矩阵:
b =零(size(a));
然后分配B中的非零数据。
祝福,
斯宾塞
1条评论
Ali Tawfik
Ali Tawfik 2020年1月27日
嗨,斯宾塞,
感谢你的及时回复。
我的意思是,我已经有一个矩阵,我想创建一个基于先前获得的矩阵的新矩阵,然后在矩阵中分配零,并在第一个矩阵中使用一些元素,
For example:
x = [1 2 3 4 5 6;
7 8 9 10 11 12;
13 14 15 16 17 18;
19 20 21 22 23 24;
25 26 27 28 29 30;
31 32 33 34 35 36]
然后,我想创建Y作为以下内容:
y = [x(1,1)x(1,4)0 0;
x(1,4)x(4,4)0 0;
0 0 x(3,3)x(6,3);
0 0 x(6,3)x(6,6)]
我创建了0(4)所以我有新矩阵,我什么estion is how to assign some elements to the new matrix to create Y ???
谢谢,

登录发表评论。


辛达
辛达 2020年1月29日
我仍然不太了解该模式,但是您可以手动插入元素:
x = [1 2 3 4 5 6;
7 8 9 10 11 12;
13 14 15 16 17 18;
19 20 21 22 23 24;
25 26 27 28 29 30;
31 32 33 34 35 36];
y =零(4);
Y(1,1) = x(1,1);
Y(1,2) = x(1,4);
y(2,1)= x(1,4);
y(2,2)= x(4,4);
y(3,3)= x(3,3);
y(3,4)= x(6,3);
y(4,3)= x(6,3);
y(4,4)= x(6,6);
% or
y =零(4);
y(1:2,1:2)= [x(1,1)x(1,4);x(1,4)x(4,4)];
y(3:4,3:4)= [x(3,3)x(6,3);x(6,3)x(6,6)];

社区寻宝

在Matlab Central中找到宝藏,发现社区如何为您提供帮助!

Start Hunting!