Unexpected behaviour of table2array
5 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Walter Roberson
2018-3-20
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 个评论
Walter Roberson
2018-3-20
That table works out as character < integer < single < double with the left-most used in the expression being the result
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!