Repeating Matrix values along a diagonal

9 visualizaciones (últimos 30 días)
Buster
Buster el 13 de Sept. de 2021
Respondida: Walter Roberson el 13 de Sept. de 2021
So lets say you have a matrix A=zeros(9,9). I have the main diagonal (and other diagonals) with values of 1 going throughout the matrix. How could i get the diagonals to skip every say 3 values. So that A(1,1), A(2,2) are 1 but A(3,3) is 0 A(4,4), A(5,5) are 1 A(6,6) is 0. The main issue is i have to do this for a 400x400 matrix or i would just hardcode these values as such but that would take too long. All of this is being done in matlab.

Respuestas (1)

Walter Roberson
Walter Roberson el 13 de Sept. de 2021
N = 400;
temp = ones(1,N); temp(3:3:end) = 0;
A(1:N+1:end) = temp;
In the case where N is exactly divisible by the length of the pattern, use repmat(), such as
pat = [1 1 0];
repmat(pat, 1, N/length(pat)) %would work for N = 399 or 402 but not N = 400

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by