Splitting a table using varagin
조회 수: 7 (최근 30일)
이전 댓글 표시
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);
채택된 답변
Stephen23
2021년 12월 5일
편집: Stephen23
2021년 12월 5일
"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
Adam Danz
2021년 12월 5일
> 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.
추가 답변 (1개)
Adam Danz
2021년 12월 5일
편집: Adam Danz
2021년 12월 5일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!