create a matrix from matrix with zeros elements

조회 수: 1 (최근 30일)
Ali Tawfik 2020년 1월 24일
답변: Sindar 2020년 1월 29일
I have a matrix obtained
A=[ 1 2 3 4;5 6 7 8;9 10 11 12; 13 14 15 16];
So I just would like to obtain another matrix but with some indexs (or elements ) and the rest is zeros
So I need the output matrix to be
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 ?
Thanks,
댓글 수: 2없음 표시없음 숨기기
Sindar 2020년 1월 24일
what is the logic of which elements are zero, and what value the non-zero ones take?
Ali Tawfik 2020년 1월 27일
Hi Sindar,
Well ,I try to explain you more,
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]
Then, I wanna create Y as the following:
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)]
I created zeros(4) so I have the new matrix, my question is how to assign some elements to the new matrix to create Y ???
Thanks,

댓글을 달려면 로그인하십시오.

답변 (2개)

Spencer Chen 2020년 1월 24일
You can create a new matrix of the same size:
B = zeros(size(A));
Then assign non-zero data in B.
Blessings,
Spencer
댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Ali Tawfik 2020년 1월 27일
Hi Spencer,
I meant, 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]
Then, I wanna create Y as the following:
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)]
I created zeros(4) so I have the new matrix, my question is how to assign some elements to the new matrix to create Y ???
Thanks,

댓글을 달려면 로그인하십시오.

Sindar 2020년 1월 29일
I still don't quite see the pattern, but you can insert elements manually:
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 = zeros(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 = zeros(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)];

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by