Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 776
strategos Avatar asked Jul 25 '14 02:07

strategos


People also ask

How do you do transpose of a table in Matlab?

If the goal is to transpose the data in the table, it's a safe bet that all the data are the same type. Use table2array and array2table instead. Beginning in R2018a, the best way to to this is rows2vars, as that method deals with things like variable names.

How do you write the transpose of a vector V in Matlab?

Matlab - The Complete Course The transpose operation changes a column vector into a row vector and vice versa. The transpose operation is represented by a single quote (').

How do I convert a table to a cell in Matlab?

Description. C = table2cell( T ) converts the table or timetable, T , to a cell array, C . Each variable in T becomes a column of cells in C .

How do you flip columns and rows in Matlab?

B = flipud( A ) returns A with its rows flipped in the up-down direction (that is, about a horizontal axis). If A is a column vector, then flipud(A) returns a vector of the same length with the order of its elements reversed. If A is a row vector, then flipud(A) simply returns A .


2 Answers

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!

like image 77
rayryeng Avatar answered Nov 17 '22 06:11

rayryeng


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  
like image 39
Oleg Avatar answered Nov 17 '22 06:11

Oleg