Zero Padding a Table

41 ビュー (過去 30 日間)
Robyn Seery
Robyn Seery 2021 年 4 月 28 日
回答済み: Adam Danz 2023 年 12 月 30 日
Hi,
Quick question om zero padding tables. I would like to add a specific number of rows to my data (quite a large table).
Below is an example:
LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'};
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(LastName,Age,Smoker,Height,Weight,BloodPressure)
I tried the following first, and got this error:
All input arguments must be tables.
T = [T; zeros(5,1)]
Then tried this, and got this error:
All tables being vertically concatenated must have the same number of variables.
T = [T; table(zeros(5,1))]
Do I need to specify the variables? For my actual data, this would be hard, because there are a couple hundred different columns.

採用された回答

Walter Roberson
Walter Roberson 2021 年 4 月 29 日
LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'};
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(LastName,Age,Smoker,Height,Weight,BloodPressure)
T = 5×6 table
LastName Age Smoker Height Weight BloodPressure ___________ ___ ______ ______ ______ _____________ {'Sanchez'} 38 true 71 176 124 93 {'Johnson'} 43 false 69 163 109 77 {'Li' } 38 true 64 131 125 83 {'Diaz' } 40 false 67 133 117 75 {'Brown' } 49 true 64 119 122 80
emp = {'', nan, false, nan, nan, [nan nan]};
T1 = [T;repmat(emp,5,1)]
T1 = 10×6 table
LastName Age Smoker Height Weight BloodPressure ___________ ___ ______ ______ ______ _____________ {'Sanchez'} 38 true 71 176 124 93 {'Johnson'} 43 false 69 163 109 77 {'Li' } 38 true 64 131 125 83 {'Diaz' } 40 false 67 133 117 75 {'Brown' } 49 true 64 119 122 80 {0×0 char } NaN false NaN NaN NaN NaN {0×0 char } NaN false NaN NaN NaN NaN {0×0 char } NaN false NaN NaN NaN NaN {0×0 char } NaN false NaN NaN NaN NaN {0×0 char } NaN false NaN NaN NaN NaN

その他の回答 (1 件)

Adam Danz
Adam Danz 2023 年 12 月 30 日
Starting in MATLAB R2023b, you can use paddata to pad tables.
LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'};
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(LastName,Age,Smoker,Height,Weight,BloodPressure)
T = 5×6 table
LastName Age Smoker Height Weight BloodPressure ___________ ___ ______ ______ ______ _____________ {'Sanchez'} 38 true 71 176 124 93 {'Johnson'} 43 false 69 163 109 77 {'Li' } 38 true 64 131 125 83 {'Diaz' } 40 false 67 133 117 75 {'Brown' } 49 true 64 119 122 80
To pad a fixed number of rows,
nTotalRows = height(T) + 5; % pad 5 rows
paddata(T,nTotalRows)
ans = 10×6 table
LastName Age Smoker Height Weight BloodPressure ____________ ___ ______ ______ ______ _____________ {'Sanchez' } 38 true 71 176 124 93 {'Johnson' } 43 false 69 163 109 77 {'Li' } 38 true 64 131 125 83 {'Diaz' } 40 false 67 133 117 75 {'Brown' } 49 true 64 119 122 80 {0×0 double} 0 false 0 0 0 0 {0×0 double} 0 false 0 0 0 0 {0×0 double} 0 false 0 0 0 0 {0×0 double} 0 false 0 0 0 0 {0×0 double} 0 false 0 0 0 0
There's an option to specify the fill value which you could set to missing but this only works when all table variables have a missing indicator. This demo table contains a cell array which does not support missing.
paddata(T,nTotalRows,'FillValue',missing)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by