Repeating Matrix values along a diagonal

7 views (last 30 days)
Buster
Buster on 13 Sep 2021
Answered: Walter Roberson on 13 Sep 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.

Answers (1)

Walter Roberson
Walter Roberson on 13 Sep 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

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!