varfun for a cell array?

조회 수: 3 (최근 30일)
Angelavtc
Angelavtc 2020년 3월 31일
댓글: Angelavtc 2020년 4월 1일
Is there a way to apply varfun(@mean,A,'GroupingVariable','VariableX')to all the elements that are contained into a cell array? If yes, how?
Thanks a lot!
  댓글 수: 5
Adam Danz
Adam Danz 2020년 3월 31일
I believe OP has a 300x1 cell array where each element is one of these tables. Good call with groupfilter.
Angelavtc
Angelavtc 2020년 4월 1일
Yes, OP has a 300x1 cell array :)

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2020년 3월 31일
편집: Adam Danz 2020년 4월 1일
I didn't quite get the last part of your description but this should get you started. If you have trouble completing your goal, please elaborate.
The key is using cellfun to evaluate a function for each element of a cell array.
% C is the [n x 1] cell array containing tables with the same headers.
% Create function to be executed on each table T
tableFcn = @(T)varfun(@mean,T,'GroupingVariable','Price');
% Compute the grouped mean for each variable
A= cellfun(tableFcn, C, 'UniformOutput', false)
% Get all values where groupcount==2
selectedGroupCount = cellfun(@(T){T(T.GroupCount == 2, :)}, A);
% Convert the cell array of tables into a final table
Afinal = vertcat(selectedGroupCount{:});
  댓글 수: 7
Adam Danz
Adam Danz 2020년 4월 1일
That function outputs the coordinates of intersection points between two curves/lines defined by (x1,y1) (x2,y2) and their indices.
Since you're dealing with multiple outputs a loop is the way to go. The code below assumes you have a nx1 cell array C containing tables with headers 'x1' 'x2' 'y1' 'y2'. It creates another cell array intersectPoints the same size as C where each element is a table showing the [x0,y0,iout,jout] values.
% C is your 300x1 cell array containing tables with
% headers 'x1' 'x2' 'y1' 'y2'
% Pre-allocate loop variables
intersectPoints = cell(size(C));
%Loop through each cell
for i = 1:numel(C)
[x0,y0,iout,jout] = intersections(C{i}.x1, C{i}.y1, C{i}.x2, C{i}.y2, 'robust');
% Put outputs in a table
intersectPoints{i} = array2table([x0,y0,iout,jout], 'VariableNames', ...
{'x0','y0','iout','jout'});
end
Angelavtc
Angelavtc 2020년 4월 1일
@Adam Danz thank you so much! this is exactly what I need it... I will give it a try :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by