如何在非方阵中获取方阵的条目

How to obtain an entry of a square matrix inside a non square matrix

我正在尝试打印某个任意形状矩形矩阵的最大方形子矩阵的最后一个元素。我对这个任务有几个提示:

Set the variable y to be the last diagonal entry of A. Since A may not be square, you will need to determine whether the last diagonal entry of A is $a_{mm}$ or $a_{nn}$.

Set the variable B to be the (square) matrix containing either the first m columns of A (if m is less than n), or the first n rows of A otherwise.

我试过 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