Repeating Matrix values along a diagonal

조회 수: 9 (최근 30일)
Buster
Buster 2021년 9월 13일
답변: Walter Roberson 2021년 9월 13일
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.

답변 (1개)

Walter Roberson
Walter Roberson 2021년 9월 13일
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

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by