Efficiently add missing parts to the table

1 visualización (últimos 30 días)
Zeynab Mousavikhamene
Zeynab Mousavikhamene el 5 de Oct. de 2019
Editada: the cyclist el 5 de Oct. de 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

Respuesta aceptada

the cyclist
the cyclist el 5 de Oct. de 2019
Editada: the cyclist el 5 de Oct. de 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;

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by