简体   繁体   中英

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

I create a 3x3 matrix. The indexing operation works well initially.

>> 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

But if I do the indexing operation on the transposed matrix, Matlab throws an error:

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

Does anyone have ideas about this?

Do it this way:

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

In Octave, you can actually do this.

Note: This does not work in 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

Simple answer, this syntax isn't allowed (in Matlab, actually it is in Octave as another answer points out). You could do the following though for the same result

K(1,:)'

Or

K = K';
K(:,1)

This won't br too expensive as matlab just flips the indices internally to do the transpose. Like the other answerer states, use .' for complex data or just as a good habit (why mathworks? Why?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM