Dot notation and curly braces with TABLE
Ältere Kommentare anzeigen
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 Kommentar
Akzeptierte Antwort
Weitere Antworten (1)
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 Kommentare
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}(:))
Jean-Marie Sainthillier
am 10 Jun. 2023
Bearbeitet: Jean-Marie Sainthillier
am 10 Jun. 2023
Kategorien
Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!