Round all values in table

조회 수: 41 (최근 30일)
Dion Theunissen
Dion Theunissen 2021년 10월 25일
댓글: Andrew Sandeman 2025년 7월 15일
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
Scott MacKenzie 2021년 10월 25일
편집: Scott MacKenzie 2021년 10월 25일
I think this achieves what you are after:
% test data
T1 = array2table(rand(5))
T1 = 5×5 table
Var1 Var2 Var3 Var4 Var5 _________ _______ _______ ________ ________ 0.0024079 0.97172 0.41667 0.027209 0.079337 0.14919 0.17995 0.58257 0.46413 0.99968 0.56165 0.79083 0.06821 0.18682 0.23824 0.31412 0.6835 0.82666 0.49151 0.12438 0.78186 0.66873 0.07059 0.86243 0.32011
% rounded to 2 decimal places
T2 = array2table(round(T1{:,:},2))
T2 = 5×5 table
Var1 Var2 Var3 Var4 Var5 ____ ____ ____ ____ ____ 0 0.97 0.42 0.03 0.08 0.15 0.18 0.58 0.46 1 0.56 0.79 0.07 0.19 0.24 0.31 0.68 0.83 0.49 0.12 0.78 0.67 0.07 0.86 0.32

Andrew Sandeman
Andrew Sandeman 2025년 7월 14일
Another solution using varfun()
% test data
T = array2table(rand(5))
T = 5×5 table
Var1 Var2 Var3 Var4 Var5 __________ _________ _________ ________ ________ 0.10584 0.27303 0.0067048 0.58837 0.063311 0.45211 0.12874 0.22154 0.4381 0.57343 0.59635 0.64793 0.89923 0.64467 0.48561 0.75997 0.0090159 0.33324 0.42146 0.60051 0.00011342 0.11944 0.35647 0.059849 0.066758
% round data in table to 2 d.p.
varfun(@(x)round(x,2), T)
ans = 5×5 table
Fun_Var1 Fun_Var2 Fun_Var3 Fun_Var4 Fun_Var5 ________ ________ ________ ________ ________ 0.11 0.27 0.01 0.59 0.06 0.45 0.13 0.22 0.44 0.57 0.6 0.65 0.9 0.64 0.49 0.76 0.01 0.33 0.42 0.6 0 0.12 0.36 0.06 0.07
The only difference is the variable names.
  댓글 수: 4
Walter Roberson
Walter Roberson 2025년 7월 14일
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))
t = 5×3 table
Var1 Var2 Var3 _______ _______ ____ 0.89086 0.66185 "a" 0.83846 0.33959 "a" 0.78412 0.42468 "a" 0.34779 0.68343 "a" 0.49041 0.28647 "a"
outvars = t.Properties.VariableNames(varfun(@isnumeric,t,OutputFormat="uniform"))
outvars = 1×2 cell array
{'Var1'} {'Var2'}
t(:,outvars) = varfun(@(x)round(x,2), t, InputVariables=outvars)
t = 5×3 table
Var1 Var2 Var3 ____ ____ ____ 0.89 0.66 "a" 0.84 0.34 "a" 0.78 0.42 "a" 0.35 0.68 "a" 0.49 0.29 "a"
Andrew Sandeman
Andrew Sandeman 2025년 7월 15일
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))
t = 5×3 table
Var1 Var2 Var3 _______ ________ ____ 0.45139 0.33976 "a" 0.6767 0.59527 "a" 0.46407 0.070445 "a" 0.87703 0.88928 "a" 0.30553 0.68711 "a"
conditionalVarfun(t, @isnumeric, @(x)round(x,2))
ans = 5×3 table
Var1 Var2 Var3 ____ ____ ____ 0.45 0.34 "a" 0.68 0.6 "a" 0.46 0.07 "a" 0.88 0.89 "a" 0.31 0.69 "a"
%% 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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by