# How to add zeros diagonally in a matrix?

6 visualizaciones (últimos 30 días)
Haya Ali el 7 de Mayo de 2023
Movida: VBBV el 9 de Mayo de 2023
I am trying to add zeros diagonally in this matrix so it should become 23*23 matrix. But with my program it becomes 24*22 matrix. Please help to resolve the code.
close all; clear all; clc;
A= [0 0 0 1 0 -1 0 -1 -1 0 1 -1 0 0 1 -1 0 1 0 1 -1 0
0 1 1 1 0 0 -1 0 0 0 1 -1 1 -1 -1 1 0 -1 0 -1 1 0
-1 -1 0 0 0 0 1 0 1 0 0 -1 1 1 -1 1 0 -1 1 -1 1 0
1 -1 0 -1 0 -1 -1 0 0 -1 1 0 0 0 -1 1 -1 0 -1 -1 0 1
0 1 -1 0 0 -1 1 -1 -1 -1 1 0 0 0 0 -1 0 0 -1 1 0 0
1 0 0 1 0 -1 -1 0 0 0 1 -1 0 0 -1 1 -1 0 -1 -1 1 1
-1 0 -1 0 1 0 1 0 1 0 -1 -1 1 1 0 0 0 0 1 1 0 0
0 0 -1 0 0 0 1 0 1 0 0 1 -1 0 1 0 -1 1 1 -1 1 -1
1 0 0 0 -1 0 0 1 1 -1 -1 -1 0 0 -1 -1 -1 0 0 -1 1 0
1 0 1 0 1 0 0 1 0 1 1 -1 1 0 -1 0 0 0 0 -1 -1 0
0 0 -1 -1 0 0 0 -1 0 -1 1 1 0 1 -1 1 -1 -1 -1 -1 0 0
0 -1 -1 -1 1 0 0 -1 0 0 0 1 1 0 -1 1 0 0 -1 -1 0 1
0 0 1 0 -1 0 -1 0 0 1 -1 -1 0 -1 0 -1 0 -1 1 1 1 1
-1 0 0 1 1 0 -1 -1 0 -1 -1 1 -1 -1 0 0 1 0 1 1 0 1
0 -1 -1 0 1 0 -1 -1 0 -1 0 0 -1 0 -1 1 0 0 1 0 0 -1
0 -1 0 -1 -1 0 0 -1 0 0 0 -1 1 1 0 1 0 0 -1 -1 0 0
-1 0 -1 -1 1 0 0 1 0 1 0 -1 1 1 1 -1 0 -1 0 0 -1 -1
-1 0 0 1 0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 1 0 0 -1 -1 0 0
-1 0 -1 -1 1 0 0 1 0 1 0 -1 1 1 1 -1 0 -1 0 0 -1 -1
-1 0 0 1 0 -1 -1 -1 0 -1 -1 1 -1 0 0 0 0 -1 1 1 1 1
0 -1 -1 0 1 0 -1 -1 0 -1 0 0 1 0 0 1 0 0 1 1 1 -1
1 1 -1 0 -1 0 -1 1 0 1 -1 -1 -1 0 1 0 -1 -1 0 1 0 -1
0 -1 1 1 0 0 -1 -1 1 1 0 -1 -1 1 0 -1 1 0 -1 0 1 0
0 -1 -1 0 1 0 0 1 0 1 0 0 -1 1 0 1 1 0 -1 0 -1 -1
1 1 -1 1 -1 0 0 0 -1 1 0 0 -1 0 -1 -1 -1 0 1 1 1 1];
S = size(A)+[0,1];
B = zeros(S);
B(~eye(S)) = A;
B
B = 23×23
0 1 -1 1 0 1 -1 0 -1 0 -1 -1 1 0 0 -1 0 0 0 -1 -1 1 0 0 0 1 -1 1 -1 0 -1 1 0 -1 -1 -1 1 1 -1 -1 -1 0 -1 1 0 0 0 0 0 -1 0 0 0 -1 -1 0 0 0 1 0 0 -1 0 -1 1 1 -1 0 1 -1 1 0 0 1 1 0 -1 -1 0 1 0 0 -1 -1 -1 1 0 0 1 1 1 0 1 -1 1 1 0 -1 0 -1 1 0 -1 0 -1 -1 -1 0 1 -1 0 0 -1 0 1 0 -1 0 1 0 0 0 0 -1 0 -1 -1 -1 1 0 0 -1 0 -1 0 -1 1 0 1 1 0 0 0 -1 0 0 1 1 1 0 1 1 1 -1 0 0 0 -1 -1 1 -1 -1 0 -1 -1 0 0 0 0 0 0 1 -1 0 -1 1 0 1 1 -1 -1 -1 -1 0 0 0 0 0 0 0 -1 -1 0 -1 1 0 -1 1 0 1 1 0 0 1 -1 0 0 1 0 -1 1 0 -1 1 0 -1 0 1 0 -1 -1 0 0 -1 0 0 1 1 0 0
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by