How to get mean of all variables in a table?

30 Ansichten (letzte 30 Tage)
Kazi Alam
Kazi Alam am 9 Apr. 2021
Bearbeitet: Kazi Alam am 9 Apr. 2021
Hello, A table ('kub') that I am trying to analyse has a dimension of 1882x33. Its a mixed table with double and strings values. The 1st and 2nd variable is contains strings. Therefore, I used the following line of code to get the mean of all Variables except 1st and 2nd.
func = @mean;
B = varfun(func,kub{:,3:end});
However, I am getting the following error.
Check for missing argument or incorrect argument data type in call to function 'varfun'.+
Can anyone help me?

Akzeptierte Antwort

Steven Lord
Steven Lord am 9 Apr. 2021
The varfun function requires one of its inputs to be a table or timetable array (or a tall table or timetable.)
which -all varfun
/MATLAB/toolbox/matlab/datatypes/tabular/@tabular/varfun.m % tabular method /MATLAB/toolbox/matlab/bigdata/@tall/varfun.m % Shadowed tall method
In your call the first input is a function handle. When you index into a table or timetable array using curly braces what you receive is not itself a table or timetable.
T = array2table(magic(4))
T = 4×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
smallerTable = T(2:3, 2:4) % table array
smallerTable = 2×3 table
Var2 Var3 Var4 ____ ____ ____ 11 10 8 7 6 12
doublePiece = T{2:3, 2:4} % double array
doublePiece = 2×3
11 10 8 7 6 12
If you want to compute the mean of all the elements of the doublePiece variable don't use varfun.
M1 = mean(doublePiece, "all")
M1 = 9
M2 = mean(doublePiece, 1)
M2 = 1×3
9 8 10
M3 = mean(doublePiece, 2)
M3 = 2×1
9.6667 8.3333
But if you want to operate on just the numeric variables in your table, you can use a function handle to select as the input variables only those that are numeric.
T.stringvar = ["a"; "b"; "c"; "d"]
T = 4×5 table
Var1 Var2 Var3 Var4 stringvar ____ ____ ____ ____ _________ 16 2 3 13 "a" 5 11 10 8 "b" 9 7 6 12 "c" 4 14 15 1 "d"
varfun(@mean, T, 'InputVariables', @isnumeric)
ans = 1×4 table
mean_Var1 mean_Var2 mean_Var3 mean_Var4 _________ _________ _________ _________ 8.5 8.5 8.5 8.5
  1 Kommentar
Kazi Alam
Kazi Alam am 9 Apr. 2021
Bearbeitet: Kazi Alam am 9 Apr. 2021
varfun(@mean, T, 'InputVariables', @isnumeric)
It worked like a charm. Thank you @Steven Lord.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by