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!
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.
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 (').
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 .
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 .
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With