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

15 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2023-5-17
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 个评论
Keith Goatman
Keith Goatman 2024-8-13,9:15
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.

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2024-8-13,17:39
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.

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by