Splitting a table using varagin
Mostrar comentarios más antiguos
I have a table named data. I want to split it into three different tables based on variables in the first column. I implemented the following lines of code which work perfectly. However, I don't understand what the varagin function is doing. The matlab documentation states that the first term in the bracket after splittaply should be a function such as @max. Is varagin acting as some sort of function? Secondly, why is it written twice (@varagin and the varagin again).
Group = findgroups(Data{:, 1});
Split = splitapply( @(varargin) varargin, Data , Group);
3 comentarios
Adam Danz
el 5 de Dic. de 2021
This is really clever. Mind sharing where you saw this idea? I'll add an explanation below as an answer.
Doron Joffe
el 5 de Dic. de 2021
Editada: Doron Joffe
el 5 de Dic. de 2021
Note that keeping data together often makes it easier to analyze, e.g.:
Respuesta aceptada
Más respuestas (1)
The function definition in splitapply can also be an anonymous function in the form @(vars)func(vars) where vars can be a single or multiple variables and func is a function. But in this case, the anonymous function takes the form @(x)x which means that the function merely returns the input. varargin is an array of inputs.
Examples:
fun = @(x)x;
fun(pi)
fun2 = @(varargin)varargin;
fun2(1,2,'S')
splitapply splits variables within a table into groups and applies the specified function. Each variable (column) of a table is treated as a separate input variable so the splitapply function must have the same number of inputs as the number of table variables. varargin is a flexible way to accept all table variables without relying on knowing the table width.
The output for an nx3 table with 2 different groups would be a 2x3 cell array C where C{i,j} contains the values in table column j that correspond to group i.
1 comentario
Doron Joffe
el 6 de Dic. de 2021
Categorías
Más información sobre Tables en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!