Splitting a table using varagin

9 Ansichten (letzte 30 Tage)
Doron Joffe
Doron Joffe am 5 Dez. 2021
Kommentiert: Doron Joffe am 6 Dez. 2021
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 Kommentare
Doron Joffe
Doron Joffe am 5 Dez. 2021
Bearbeitet: Doron Joffe am 5 Dez. 2021
Thank you very much. I would appreciate any help on this code. I saw the code at this link.
https://www.mathworks.com/matlabcentral/answers/440184-is-it-possible-to-split-a-table-into-multiple-tables-based-in-id-a-number-code-in-colum-a#answer_356786

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 5 Dez. 2021
Bearbeitet: Stephen23 am 5 Dez. 2021
"The matlab documentation states that the first term in the bracket after splittaply should be a function such as @max"
The SPLITAPPLY documentation actually states that the first input must be a function handle:
states that the first input as: "func — Function to apply to groups of data ... function handle ... Function to apply to groups of data, specified as a function handle."
Function handles are one of the fundamental data classes in MATLAB:
"Is varagin acting as some sort of function?"
No, VARARGIN is a special input argument in a function definition (aka signature) which collects any number of inputs into one cell array which is named varargin:
"Secondly, why is it written twice (@varagin and the varagin again)."
Because this code:
@(varargin)varargin
defines an anonymous function:
which uses VARARGIN to accept any number of inputs and stores them all in a cell array. Whilst it is certainly possible to write functions which totally ignore their inputs, if you want to use the inputs in some way then they need to be referred to within the function itself. So VARARGIN occurs as an input to the anonymous function and also when it is used (which in this example is simply for the cell array itself, rather then the more common cases of passing the data to another function call or something similar).
Why it works is explained in the source answer:
  4 Kommentare
Adam Danz
Adam Danz am 5 Dez. 2021
> would the inputs to varargin be the numbers 3,2,1 etc from the findgroup function
No. In splitapply(@(varargin)varargin,Data,Group), Data are the inputs (see my answer). The grouping variable is applied within splitapply, not within the anonymous function.
Doron Joffe
Doron Joffe am 6 Dez. 2021
Thank you for the excellent explanation.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Adam Danz
Adam Danz am 5 Dez. 2021
Bearbeitet: Adam Danz am 5 Dez. 2021
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)
ans = 3.1416
fun2 = @(varargin)varargin;
fun2(1,2,'S')
ans = 1×3 cell array
{[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 Kommentar
Doron Joffe
Doron Joffe am 6 Dez. 2021
thank you very much. This explains it perfectly.

Melden Sie sich an, um zu kommentieren.

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