Efficiently add missing parts to the table

1 回表示 (過去 30 日間)
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019 年 10 月 5 日
編集済み: the cyclist 2019 年 10 月 5 日
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 Center および File ExchangeGraphics Object Properties についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by