【问题标题】:In Matlab/Octave, how to do indexing for a transposed matrix?在 Matlab/Octave 中,如何对转置矩阵进行索引?
【发布时间】:2017-04-08 21:11:38
【问题描述】:

我创建了一个 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 matrix octave


    【解决方案1】:

    这样做:

    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
    

    【讨论】:

      【解决方案2】:

      在 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
      

      【讨论】:

        【解决方案3】:

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

        K(1,:)'
        

        或者

        K = K';
        K(:,1)
        

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

        【讨论】:

          猜你喜欢
          • 2015-07-10
          • 2018-09-09
          • 1970-01-01
          • 1970-01-01
          • 2021-09-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多