Efficiently add missing parts to the table

1 view (last 30 days)
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

Accepted Answer

the cyclist
the cyclist on 5 Oct 2019
Edited: the cyclist on 5 Oct 2019
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;

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!