strategos
strategos

Reputation: 75

Matlab transpose a table vector

Seems like an embarassingly simple question, but how can I transpose a Matlab table vector?

For a simple transposition of a column vector aTable to a row vector I tried standard syntaxes:

aTableT = aTable.';

aTableT = reshape(aTable, 1, height(aTable));

and

aTableT = rot90(aTable);

According to Mathworks the last one should work for table array, see here. However, I get this error code:

Error using table/permute (line 396) Undefined function 'permute' for input arguments of type 'table'.

Error in rot90 (line 29) B = permute(B,[2 1 3:ndims(A)]);

NB: fliplr isn't useful either. Pretty sure I've covered the obvious angles - any ideas? thanks!

Upvotes: 6

Views: 7421

Answers (2)

Oleg
Oleg

Reputation: 10676

I extend the table() class with a few additional methods and fix some existing problems in https://github.com/okomarov/tableutils.

Specific to this question, I define a transpose of a matrix-like table, i.e. if the table has one value per cell, and all variables (columns) are of the same class, then you can transpose the table with my package.

An example:

t = array2table(magic(4))
t = 
    Var1    Var2    Var3    Var4
    ____    ____    ____    ____
    16       2       3      13  
     5      11      10       8  
     9       7       6      12  
     4      14      15       1  
>> t'
ans = 
            Row1    Row2    Row3    Row4
            ____    ____    ____    ____
    Var1    16       5       9       4  
    Var2     2      11       7      14  
    Var3     3      10       6      15  
    Var4    13       8      12       1  

Upvotes: 2

rayryeng
rayryeng

Reputation: 104504

Try converting your table into an array, transposing that, then convert back to a table. In other words, try doing this:

aTableArray = table2array(aTable);
aTableT = array2table(aTableArray.');

I read the documentation for rot90 too, and it says that rot90 should definitely work for tables, and I get the same error as you. As such, since transposing obviously works for arrays / matrices, let's do a quick workaround by converting to a matrix, transposing that, then converting back to a table. This worked for me!

Upvotes: 3

Related Questions