how to transform a table with column names into an double array without column names

44 Ansichten (letzte 30 Tage)
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
  1. Using 'X(2:end,:)' but it removes the first row of the numeric entries instead of the column names;
  2. 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!!

Antworten (4)

VBBV
VBBV am 25 Mär. 2023
X = table2array(X(:,2:end))
Try as above

Atsushi Ueno
Atsushi Ueno am 25 Mär. 2023
Verschoben: Image Analyst am 26 Mär. 2023
I cannot reproduce the problem. table2array does work as expected in above document.
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"])
T = 5×4 table
Smoker Age Height Weight ______ ___ ______ ______ Y 38 71 176 Y 43 69 163 N 38 64 131 N 40 67 133 F 49 64 119
A = table2array(T(:,2:4))
A = 5×3
38 71 176 43 69 163 38 64 131 40 67 133 49 64 119

Stephen23
Stephen23 am 25 Mär. 2023
Verschoben: Image Analyst am 26 Mär. 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 = 1×2
1.0e+07 * 1.9720 -0.0000
M(2) % of course not, that is just how data are *displayed*, here is that value
ans = -0.3587
You could also change the FORMAT, which changes how numeric data are displayed:
format short E
M
M = 1×2
1.0e+00 * 1.9720e+07 -3.5874e-01
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
Image Analyst am 26 Mär. 2023
Bearbeitet: Image Analyst am 26 Mär. 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().

Produkte


Version

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by