Problem with direct calculation on table with std and "omitnan"

8 views (last 30 days)
Since R2023a, it is possible to perform calculations directly on tables (and timetables) without extracting their data by indexing.
I want use std directly on a numeric table where I can have nan.
For example :
load patients
T = table(Age,Height,Weight,Systolic,Diastolic)
mean(T,"omitnan")
It's fine.
But why there is a problem with std(T,"omitnan") ?
% Applying the function 'std' to the variable 'Age' generated an error.
I can use std(T{:,:},"omitnan") or std(T.Variables,"omitnan") but I lost the possibility to work directly with my table.
Did I miss something ?
Do you have any suggestion ?
Thank you in advance.
SAINTHILLIER Jean Marie

Accepted Answer

Star Strider
Star Strider on 17 May 2023
Prehaps varfun?
Example —
load patients
T = table(Age,Height,Weight,Systolic,Diastolic)
T = 100×5 table
Age Height Weight Systolic Diastolic ___ ______ ______ ________ _________ 38 71 176 124 93 43 69 163 109 77 38 64 131 125 83 40 67 133 117 75 49 64 119 122 80 46 68 142 121 70 33 64 142 130 88 40 68 180 115 82 28 68 183 115 78 31 66 132 118 86 45 68 128 114 77 42 66 137 115 68 25 71 174 127 74 39 72 202 130 95 36 65 129 114 79 48 71 181 130 92
S = varfun(@(x)std(x,'omitnan'),T)
S = 1×5 table
Fun_Age Fun_Height Fun_Weight Fun_Systolic Fun_Diastolic _______ __________ __________ ____________ _____________ 7.2154 2.8365 26.571 6.7128 6.9325
.
  3 Comments
Keith Goatman
Keith Goatman on 13 Aug 2024
I too was caught out by the inconsistent behaviour of mean and std on tables in 2024a. The answer above works around it but it doesn't explain why they should behave differently.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 13 Aug 2024
This looks like a bug to me. It seems that std for table arrays assumes that if you're passing the missingflag input that you've also specified the normalization option or weights. But specifying just the data and the missingflag input is a supported syntax for a double array input and so probably should be supported for a table array input.
A workaround is to specify the default normalization option (0) in the std call as the second input.
load patients
T = table(Age,Height,Weight,Systolic,Diastolic);
mean(T,"omitnan")
ans = 1x5 table
Age Height Weight Systolic Diastolic _____ ______ ______ ________ _________ 38.28 67.07 154 122.78 82.96
std(T, 0, "omitnan")
ans = 1x5 table
Age Height Weight Systolic Diastolic ______ ______ ______ ________ _________ 7.2154 2.8365 26.571 6.7128 6.9325
% check
std(T.Age, "omitnan")
ans = 7.2154
I'll report this to the development staff.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!