Efficiently add missing parts to the table

1 vue (au cours des 30 derniers jours)
Zeynab Mousavikhamene
Zeynab Mousavikhamene le 5 Oct 2019
Modifié(e) : the cyclist le 5 Oct 2019
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

Réponse acceptée

the cyclist
the cyclist le 5 Oct 2019
Modifié(e) : the cyclist le 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;

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by