Repeating Matrix values along a diagonal
9 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!