Dot notation and curly braces with TABLE
조회 수: 21 (최근 30일)
이전 댓글 표시
I don't understand clearly the difference between T.(expression) and T{:,expression} notation.
For example :
load patients
data = table(Gender,Age)
varfun(@class, data)
What's the matter with :
data{:,2} = uint8(data{:,2});
No error, no warning, everything seems OK, but Age is always double. Why ?
varfun(@class, data)
I understand that I must use :
data.(2) = uint8(data{:,2});
varfun(@class, data)
Why data.(2) is different of data{:,2} to modify the type of variables.
Thank you advance.
SAINTHILLIER Jean Marie
댓글 수: 1
채택된 답변
Stephen23
2023년 6월 9일
편집: Stephen23
2023년 6월 10일
You can think of curly-brace indexing into a table as being something a bit like parenthesis indexing into a numeric array:
V = 1:7
V(:) = uint8(11:17) % indexing into V does not change its class!
In contrast, using dot indexing to assign to a table basically reassigns the entire variable, much like this:
V = uint8(11:17)
This behavior is expected: curly-brace indexing can be used to index into some of the table rows, so we do not expect indexing using curly braces to change the data type in that table column/variable:
T = array2table((1:7).')
T{3:5,1} = uint8(13:15).'
class(T.Var1)
Clearly it would be inconsistent of MATLAB to suddenly change that behavior when all elements are being indexed into.
Dot indexing is quite a different thing, because users expect to be able to replace an entire table column/variable with
tbl.name = whatever
and it should work (even if there already exists a completely different, incompatiable class in a variable/column with that name)... in fact, given that requirement, this is the only interpretation of this syntax that is consistent with new table columns/variables and existing ones (otherwise the behavior would be different, depending on if that variable/column exists already or not).
T.Var1 = uint8(11:17).'
class(T.Var1)
댓글 수: 2
Peter Perkins
2023년 7월 17일
Stephen23 should get extra extra reputation point for this one!
"curly-brace indexing can be used to index into some of the table rows"
vs.
"dot indexing to assign to a table basically reassigns the entire variable"
I will add that t.(expression) only extracts ONE variable at a time, while t{:,expression} can extract multiple vars as one array, if expression is like 1:3 or ["a" "b" "c"]. Check out https://www.mathworks.com/help/matlab/matlab_prog/computations-with-numeric-data-in-table-or-timetable.html for more about this, with the caveat that for R2023a and later, you should also read https://www.mathworks.com/help/matlab/matlab_prog/direct-calculations-on-tables-and-timetables.html and a couple of its siblings.
추가 답변 (1개)
Matt J
2023년 6월 9일
편집: Matt J
2023년 6월 9일
In Matlab, there are circumstances in which indexed assignments will convert the type of the right hand side in order to be consistent with the rest of the data in the variable. Leaving aside tables for a moment, we can see this with simple double vectors, e.g.,
x=1:5; class(x)
x(1)=uint8(7), class(x)
The vector x is still of class double because when I assign uint8(7) to x(1), the operation automatically converts uint8(7) to double(7) because the rest of the x(i) are already doubles, and this conversion needs to be done so that they are left unaffected by the assignment.
You are seeing something analogous in the case of tables. It is the same as if you had done,
load patients
data = table(Gender,Age);
for i=1:height(data)
data{i,2} = uint8(data{i,2});
end
In each pass through the loop uint8(data{i,2}) will be converted to double for the same reason as with the double vector x. The syntax data.(2) =___ is a special syntax designed for tables which allows you to rebuild the entire table column from scratch.
댓글 수: 2
Matt J
2023년 6월 9일
Note that not all indexed assignments result in a conversion, because some variables are treated as containers for other variables. Consider the case of a cell array,
C={[1;2;3],'dog'}
This indexed assignment will completely overwrite C{1} with data of a new class,
C{1}=uint8(C{1})
whereas this will leave it untouched,
C{1}(:)=double(C{1}(:))
참고 항목
카테고리
Help Center 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!