Repeating Matrix values along a diagonal

4 ビュー (過去 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 Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by