How to create a square matrix with zero on main diagonal and all 1's, 2's, 3's, ... n's on upper and lower diagonals?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
I have to create a square matrix using a diag function that has the pattern:
m = [ 0 1 2 3 4.... ;
1 0 1 2 3 .... ;
2 1 0 1 2 ....]
The issue is with the line m = m + diag(n, i). It doesn't have the right dimensions. Any feedback would be appreciated!
function m = matpat(n)
%UNTITLED5 Summary of this function goes here
% Function creates an n x n matrix with a pattern of 0 on main diagonal,
% and all 1s, 2s, 3s, ... on upper and lower diagonals
m = zeros(n) ;
for i = 1 : (n - 1)
m = m + diag(n , i) ;
end
0 Kommentare
Antworten (1)
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!