How can I create a matlab matrix below?

1 次查看(过去 30 天)
Hi, I've been wanting to construct a matlab matrix that is n by n. and looks like this. I've beeen trying to use diag but the function diag doesn't provide this. so m a bit confused. Please help!

采纳的回答

Walter Roberson
Walter Roberson 2021-3-3
N = 6;
diag(-2*ones(1,N)) + diag(ones(1,N-1),-1) + diag(ones(1,N-1),1)
ans = 6×6
-2 1 0 0 0 0 1 -2 1 0 0 0 0 1 -2 1 0 0 0 0 1 -2 1 0 0 0 0 1 -2 1 0 0 0 0 1 -2

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by