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
Guillaume
2018년 3월 20일
편집: Guillaume
2018년 3월 20일
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!
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!