Efficiently add missing parts to the table

2 次查看(过去 30 天)
In the attached table, radius ranges from 5 to 26. Some radii are not in the table for example radius=1 to 4, or radius=7 or 8 and so on. I want to know how can I add missing radius efficiently? Other columns of those added radii would be zero.
Capture.JPG

采纳的回答

the cyclist
the cyclist 2019-10-5
编辑:the cyclist 2019-10-5
I don't work with tables much, and this is a bit awkward, but it should work:
% Make up a bit of pretend data, and create a table from it
rng(3)
radius = randi(8,[3 1]);
other = randi(8,[3 1]);
tbl = table(radius,other);
% Pull out the radii (pretending that I don't already have that variable already)
radius = tbl.radius;
% Identify the missing radii
missingRadii = setdiff(1:max(radius),radius)';
% Figure out how many rows and columns to append to table
rowsToAdd = numel(missingRadii);
colsToAdd = size(tbl,2);
% Create a table of zeros of appropriate size
zeroTable = array2table(zeros(rowsToAdd,colsToAdd),'VariableName',{'radius','other'});
% Append the table of zeros
tbl = [tbl; zeroTable];
% Insert the missing radii
tbl.radius((end-rowsToAdd+1):end) = missingRadii;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by