Is There a Way to Execute splitapply Functionality on Subtables of Master Table?
조회 수: 12 (최근 30일)
이전 댓글 표시
Suppose I have a function that operates on a table and returns a row vector:
function rowvec = myfunc(Table)
Suppose I have a master table, call it T with one of its variables being Name. I'd like to do something like the following to group by Name and concatenate rowvec computed from each subgroup:
G = findgroups(T.Name);
R = splitapply(@myfunc,T,G);
This won't work because splitapply sends the group of each variable in T to myfunc and not the subtable of T defined by G.
Is there already a function that does what I'm trying to do?
Or do I have to use the code here: https://www.mathworks.com/matlabcentral/answers/457422-separate-table-data-in-to-sub-tables to generate the cell array of subtables, loop over the subtables with a call to myfunc, and then concatenate the rows myself? Or maybe use cellfun on the cell array of subtables?
댓글 수: 2
dpb
2020년 5월 10일
I don't follow what's wrong with splitapply?
Show us an example that doesn't do the right thing.
답변 (1개)
Peng Li
2020년 5월 10일
you'd better do this way:
splitapply(@(x) sum(x, 1), T{:, 2:3}, G);
댓글 수: 5
Peng Li
2020년 5월 11일
It is not clear why you want to subtable your table before splitapply. splitapply is supposed to finish the job for you. Anyway, I'm happy that you found out the solution for your problem!
참고 항목
카테고리
Help Center 및 File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!