Dot notation and curly braces with TABLE
22 次查看(过去 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!