how to transform a table with column names into an double array without column names
44 views (last 30 days)
Show older comments
I understand to use 'fitlm' both inputs should be array, therefore 'X' as a table needs to be transformed into array.
But using 'table2arrray' directly, entries of 'X' are all transformed into '0'. I understand this is because the column names in the first row of the table are not identified.
Here is what 'X' looks like:
I tried
- Using 'X(2:end,:)' but it removes the first row of the numeric entries instead of the column names;
- Using 'Properties.VariableNames' to reset the column names to '{}' but it gives the error again.
I need some suggestions how to fix the issue, thanks a lot!!
0 Comments
Answers (4)
Atsushi Ueno
on 25 Mar 2023
Moved: Image Analyst
on 26 Mar 2023
How did you use this function?
T = table(categorical(["Y";"Y";"N";"N";"F"]),[38;43;38;40;49],...
[71;69;64;67;64],[176;163;131;133;119],...
'VariableNames',["Smoker" "Age" "Height" "Weight"])
A = table2array(T(:,2:4))
0 Comments
Stephen23
on 25 Mar 2023
Moved: Image Analyst
on 26 Mar 2023
"But using 'table2arrray' directly, entries of 'X' are all transformed into '0'."
I doubt that. What is much more likely is that you are confusing how data are displayed with what data are actually stored in memory. Those are not the same thing at all. Note that your data has much larger values in the first column, so the default FORMAT will display the numeric matrix with one common multiplier:
M = [1.972e7,-0.35874] % is the second element really zero?
M(2) % of course not, that is just how data are *displayed*, here is that value
You could also change the FORMAT, which changes how numeric data are displayed:
format short E
M
Note that none of this makes any difference to the data which was stored in memory (which was not zero in the first place).
"I understand this is because the column names in the first row of the table are not identified."
I doubt that. Note that TABLE2ARRAY does not need to "identify" any column names.
Image Analyst
on 26 Mar 2023
Edited: Image Analyst
on 26 Mar 2023
Simply use table2array
A = table2array(X)
Note the column headers are not actually part of the data so that's why doing X(2:end, :) won't work. That will just strip off the first row of actual data, like you already observed.
Note: this assumes that all columns of X are numerical, as yours are. If some are characters/strings, then you have to extract only the numerical columns before calling table2array().
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!