Unexpected behaviour of table2array

5 visualizaciones (últimos 30 días)
George Corliss
George Corliss el 20 de Mzo. de 2018
Editada: Guillaume el 20 de Mzo. de 2018
Documentation for table2array says, "table2array creates a homogeneous array, A, of the dominant data type. For example, if T contains double and single numeric data, table2array(T) returns an array with data type single." That surprises me. I would expect double to dominate single. Here is a simple example whose results surprise me:
function table2array_surprise
Dou = 2.345;
Int = int32(2);
T = table(Dou, Int);
R = table2array(T);
fprintf('Dou : %5.3f\n', Dou(1,1));
fprintf('R(1,1): %5.3f\n', R(1,1));
end
I encounter this using R2018a database select(). If the column is int in the database, it is Matlab int in the resulting table. table2array(select(...)) truncates other float(8) columns to integers.
Documented behavior, true, but surprising.

Respuestas (1)

Walter Roberson
Walter Roberson el 20 de Mzo. de 2018
This is consistent with the MATLAB type rules. When you mix numeric data types then the rule is that the result is the most restrictive data type. I do not know why that was chosen as opposed to least restrictive, but it is the rule even just for horzcat
  4 comentarios
Walter Roberson
Walter Roberson el 20 de Mzo. de 2018
That table works out as character < integer < single < double with the left-most used in the expression being the result
Guillaume
Guillaume el 20 de Mzo. de 2018
Editada: Guillaume el 20 de Mzo. de 2018
Except, as per the links below the table, when you mix integers with or without float, the common type is the leftmost integer in the array
[single uint32 int8 double] -> all converted to uint32
[logical double int8 uint32] -> all converted to int8
Madness!

Iniciar sesión para comentar.

Categorías

Más información sobre Tables en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by