如何获得非方阵内方阵的项

时间:2019-04-28 09:31:59

标签: matrix octave diagonal matrix-indexing submatrix

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

  

将变量y设置为A的最后一个对角线条目。由于A可能不是正方形,因此您需要确定A的最后一个对角线条目是$a_{mm}$还是$a_{nn}$

     

将变量B设置为包含A的前m列(如果m小于n的前m列的(方)矩阵。否则,nA

我尝试做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或错误。

1 个答案:

答案 0 :(得分:0)

以下是几种方法:

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