我试图打印一些任意形状矩形矩阵的最大方形子矩阵的最后一个元素。我有几个提示来完成这项任务:
将变量
y
设置为A
的最后一个对角线条目。由于A
可能不是正方形,你需要确定A
的最后一个对角线条目是$a_{mm}$
还是$a_{nn}$
。将变量
B
设置为(正方形)矩阵,其中包含A
的前m列(如果m
小于n
),否则为n
的第一个A
行。
我尝试过做m
(列)和n
(行)的不同组合,例如A(1:m/n,:)
,A(:,1:m/n)
。
我也尝试使用X(m/n:m/,1/m/n:m/n)
等代码将上述两个概念结合起来。
我对如何打印最后一个方格条目感到有点困惑,因为所有这些组合都会导致错误(某些行大于列因此无效,反之亦然)或者它打印出矩阵的最后一个值,而不是方阵。
期望的结果应该给出非方矩阵的方阵中的最后一个值。
例如,如果矩阵是
$[2,3,4,6;0,1,-1,-10]$
我希望输出是1
,但我得到-10
,或错误。
以下是几种方法:
A = [2,3,4,6;0,1,-1,-10]; % Define A
[m,n] = size(A); % Get the size of A
B = A ( 1:min(n,m), 1:min(n,m) ); % Get the sub array B
d = diag(B); % Obtain the diagonal of B
lastEntry = d(end); % Obtain the last entry of the diagonal
在MATLAB中,以下也适用(跳过B
的创建):
A = [2,3,4,6;0,1,-1,-10]; % Define A
d = diag(A); % Obtain the diagonal of A
lastEntry = d(end); % Obtain the last entry of the diagonal
或这个:
A = [2,3,4,6;0,1,-1,-10]; % Define A
[m,n] = size(A); % Get the size of A
lastEntry = A ( min(n,m), min(n,m) ); % Access the relevant element