Round all values in table
古いコメントを表示
Hi,
I have a table with calculated values. Now I want to round all values to 2 decimals. How can I do that in once for the whole table?
回答 (2 件)
Scott MacKenzie
2021 年 10 月 25 日
編集済み: Scott MacKenzie
2021 年 10 月 25 日
I think this achieves what you are after:
% test data
T1 = array2table(rand(5))
% rounded to 2 decimal places
T2 = array2table(round(T1{:,:},2))
Another solution using varfun()
% test data
T = array2table(rand(5))
% round data in table to 2 d.p.
varfun(@(x)round(x,2), T)
The only difference is the variable names.
4 件のコメント
Walter Roberson
2025 年 7 月 14 日
If you use the varfun() option InputVariables=@numeric then you can have the rounding apply only to the numeric portions of the table.
However, variables not selected by the @isnumeric will simply not appear in the output table, and the variables that do appear will have 'round_' as a prefix. This is not exactly the most convenient for post-processing.
Another approach is to create a helper function along the lines of
function out = conditional_round2(in)
if isnumeric(in)
out = round(in,2);
else
out = in;
end
end
and then
new_T = varfun(@conditional_round2, T);
new_T.Properties.VariableNames = regexprep(T.Properties.VariableNames, '^[^_]*_', '', 'once');
Thanks very much, I had no idea about the InputVariables option to varfun(), something I definitely could have used in the past!
What about this, still using varfun():
% test data
t = table(rand(5,1), repelem("a",5,1))
% round data in table to 2 d.p.
t(:,"Var1") = varfun(@(x)round(x,2), t, InputVariables=@isnumeric)
The other method, whilst also good, for me coming up with that regex pattern is still a mental speedbump. However, I can see an extension to conditionalRound. I would call it a conditionalFunction, and should you want to say round to 2 d.p. if is column is numeric, do something else if isstring e.g.
function out = conditionalFunction(in)
if isnumeric(in)
out = round(in,2);
elseif isstring(in)
% do something to strings
else
out = in;
end
end
That's an interesting use of table assignment.
Needs more work if there could be multiple output variables.
t = table(rand(5,1), rand(5,1), repelem("a",5,1))
outvars = t.Properties.VariableNames(varfun(@isnumeric,t,OutputFormat="uniform"))
t(:,outvars) = varfun(@(x)round(x,2), t, InputVariables=outvars)
And here it is wrapped up in a function.
Awesome! I feel like with some documentation it might be worth putting on the file exchange?
t = table(rand(5,1), rand(5,1), repelem("a",5,1))
conditionalVarfun(t, @isnumeric, @(x)round(x,2))
%% functions
function t = conditionalVarfun(t, fh_condition, fh_operation)
% t (table)
% fh_condition e.g. @isnumeric
% fh_operation e.g. @(x)round(x,2)
p = inputParser;
p.addRequired("t", @istable)
p.addRequired("fh_condition", @isfunctionhandle)
p.addRequired("fh_operation", @isfunctionhandle)
p.parse(t, fh_condition, fh_operation)
t = p.Results.("t");
fh_condition = p.Results.("fh_condition");
fh_operation = p.Results.("fh_operation");
outvars = t.Properties.VariableNames(varfun(fh_condition,t,OutputFormat="uniform"));
t(:,outvars) = varfun(fh_operation, t, InputVariables=outvars);
end
function bool = isfunctionhandle(x)
bool = isa(x, 'function_handle');
end
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!