Matlab transpose a table vector
Asked Answered
A

2

6

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!

Ablution answered 25/7, 2014 at 2:30 Comment(0)
P
3

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!

Phellem answered 25/7, 2014 at 4:9 Comment(5)
rot90 works in 2014a, but I'm not sure if the result is the desired one, it behaves more like flip-Edwinaedwine
This is the worst type of bugs, code that is changed while documentation is not. I have found a few in matlab. In some occations I have dug into the code (when written in matlab) and found that the documented behaviour have been explicitly removed :(.Acroter
@thewaywewalk - Curious. I used 2014a for this test and I got the permute error that the OP was talking about. Talk about inconsistency!Phellem
@Phellem I just get the error if the second input is not a multiple of 90.Edwinaedwine
@thewaywewalk - Interesting. This is probably the first time I have seen this kind of bug in MATLAB. For it being licensed software and with so many investors, you'd think that an issue like this would be ironed out.Phellem
S
2

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  
Stuckey answered 26/1, 2016 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.