在Matlab/Octave中,如何对转置矩阵进行索引?

In Matlab/Octave, how to do indexing for a transposed matrix?

我创建了一个 3x3 矩阵。索引操作最初运行良好。

>> K=rand(3)

K =

    0.8147    0.9134    0.2785
    0.9058    0.6324    0.5469
    0.1270    0.0975    0.9575

>> K(:,1)

ans =

    0.8147
    0.9058
    0.1270

但是如果我对转置矩阵进行索引操作,Matlab会报错:

>> K'(:,1)
 K'(:,1)
   ↑
Error: Unbalanced or unexpected parenthesis or bracket.
>> (K')(:,1)
 (K')(:,1)
     ↑
Error: Unbalanced or unexpected parenthesis or bracket.

有人对此有想法吗?

简单的回答,这种语法是不允许的(在 Matlab 中,实际上它是在 Octave 中,正如另一个答案指出的那样)。您可以执行以下操作以获得相同的结果

K(1,:)'

或者

K = K';
K(:,1)

这不会太昂贵,因为 matlab 只是在内部翻转索引来进行转置。与其他回答者状态一样,对复杂数据使用 .' 或作为一个好习惯(为什么数学有效?为什么?)

这样做:

K(1,:).'
% note the dot above (.' - means transpose)

% however if you want Hermitian then do this
K(1,:)'
% (just ' - means Hermitian)

% well if K is real then it does not matter

在 Octave 中,您实际上可以做到这一点。

注意:这在 MATLAB 中不起作用

K =

   0.814700   0.913400   0.278500
   0.905800   0.632400   0.546900
   0.127000   0.097500   0.957500

>> (K.')(:,1)
ans =

   0.81470
   0.91340
   0.27850