96.4k views
1 vote
Hi!

i want to ask how to create this matrix A=[-4 2 1;2 -4 1;1 2 -4] using only eye ones and zeros .Thanks in advance!!

User Mr Alemi
by
8.4k points

1 Answer

7 votes

The matrix A=[-4 2 1;2 -4 1;1 2 -4] can be created by using the following code in Matlab/Octave:

A = -4*eye(3) + 2*(eye(3,3) - eye(3)) + (eye(3,3) - 2*eye(3))

Here, eye(3) creates an identity matrix of size 3x3 with ones on the diagonal and zeros elsewhere.

eye(3,3) - eye(3) creates a matrix of size 3x3 with ones on the off-diagonal and zeros on the diagonal.

eye(3,3) - 2*eye(3) creates a matrix of size 3x3 with -1 on the off-diagonal and zeros on the diagonal.

The code above uses the properties of the identity matrix and the properties of matrix addition and scalar multiplication to create the desired matrix A.

You can also create the matrix A by using following code:

A = [-4 2 1; 2 -4 1; 1 2 -4]

It is not necessary to create the matrix A using only ones and zeroes but this is one of the way to create this matrix.

User John Kenn
by
7.5k points