How to write a diagonal rectangular matrix
13 次查看(过去 30 天)
显示 更早的评论
I have to insert on matlab this matrix:
n = 100;
A = diag(21*ones(n,1)) + diag(-4*ones(n-1,1),-1)...
+ diag(-20*ones(n-1,1),1) + diag(ones(n-2,1),-2)...
+ diag(2*ones(n-2,1), 2);
I can generate it when it is squared, but now the exercise tells me to use the same numbers in a rectangular matrix having dimensions 100x98: how do I create the new matrix?
0 个评论
采纳的回答
Chunru
2023-11-23
编辑:Chunru
2023-11-23
You can use toeplitz function.
% Smaller n, m here
n = 10;
m = 8;
A = diag(21*ones(n,1)) + diag(-4*ones(n-1,1),-1)...
+ diag(-20*ones(n-1,1),1) + diag(ones(n-2,1),-2)...
+ diag(2*ones(n-2,1), 2);
whos
A
c = zeros(n, 1); c(1:3)=[21 -4 1]; % 1st column
r = zeros(m, 1); r(1:3)=[21 -20 2]; % 1st row
B = toeplitz(c, r)
3 个评论
Dyuman Joshi
2023-11-23
% Smaller n, m here
n = 10;
m = 8;
A = diag(21*ones(n,1)) + diag(-4*ones(n-1,1),-1)...
+ diag(-20*ones(n-1,1),1) + diag(ones(n-2,1),-2)...
+ diag(2*ones(n-2,1), 2)
%Remove the last n-m columns
A(:,end-(1:n-m)) = []
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!