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 comentarios

Adam Danz
Adam Danz el 31 de Mzo. de 2020
Short answer: no.
But there are other ways to achieve your goal.
Please provide samples of your data so we can see what your cell array looks like, what's stored in it, it's shape, and the grouping variable.
Angelavtc
Angelavtc el 31 de Mzo. de 2020
Ahhh I see @Adam Danz. Well, imagine that I have a cell array (with size of 300*1) where each element is like the following timetable, called A, with the same variables but different days (each element or timetable is not of the same size):
timeStamps = datetime([2017 3 4; 2017 3 5; 2017 3 4; 2017 3 4; 2017 3 4; 2017 3 4; 2017 3 4; 2017 3 4]);
Volume = [5 10 15 20 25 30 35 40]';
Price= [40 35 40 25 40 35 10 25]';
T= table(timeStamps, Volume, Price)
A= table2timetable(T)
I want to apply the varfun function to all elements, like this for the timetable A:
A_sum=varfun(@mean,A,'GroupingVariable','Price');
and then keep just the values where the GroupCount=2 (this condition is the same for all elements):
keep_A= A_sum.GroupCount>2;
keep_A= A_sum.GroupCount<2;
In the end, I am only interested in the last timetable keep_A for all the cell array.
Any Idea of how to automatize the process?
Thanks in advance!
Guillaume
Guillaume el 31 de Mzo. de 2020
I'm a bit confused by the description, there's no cell array in your example.
Note that "keep just the values where the GroupCount=2" is achieved in one line with groupfilter instead of varfun.
Adam Danz
Adam Danz el 31 de Mzo. de 2020
I believe OP has a 300x1 cell array where each element is one of these tables. Good call with groupfilter.
Angelavtc
Angelavtc el 1 de Abr. de 2020
Yes, OP has a 300x1 cell array :)

Iniciar sesión para comentar.

 Respuesta aceptada

Adam Danz
Adam Danz el 31 de Mzo. de 2020
Editada: Adam Danz el 1 de Abr. de 2020

0 votos

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 comentarios

Angelavtc
Angelavtc el 1 de Abr. de 2020
@Adam Danz thank you, this is exactly what I wanted. So I have to define the function and then apply it using cellfun.Why did you use in the computation for A ('UniformOutput', false) and not in the computation of selectedGroupCount ? what is this telling you?
Then how do you define a function that uses more than one variable as an input for the function? For example using this table V, a function that subtracts Var1 - Var2 by row?
V = table([0.71;-2.05;-0.35;-0.82;1.57],[0.23;0.12;-0.18;0.23;0.41])
Thanks in advance!
Steven Lord
Steven Lord el 1 de Abr. de 2020
What you're asking for doesn't require varfun.
V = table([0.71;-2.05;-0.35;-0.82;1.57],[0.23;0.12;-0.18;0.23;0.41]);
x = V.Var1-V.Var2
Angelavtc
Angelavtc el 1 de Abr. de 2020
Yes, maybe this example was really obvious, sorry. I am saying this because I will need to apply this fuction:
function [x0,y0,iout,jout] = intersections(x1,y1,x2,y2,robust)
Which I found on a file exchange to all my elements (timetable) that belong to my cell array. Where x1,y1,x2,y2 will be the variables that composed each timetable. I dont see clearly how to apply it.
Guillaume
Guillaume el 1 de Abr. de 2020
What does the function do? It sounds suspiciously similar to synchronize
Angelavtc
Angelavtc el 1 de Abr. de 2020
@Guillaume it finds the intersection point of two curves, where x1, y1 are the points that define the first curve and x2,y2 the points that define the second :)
Adam Danz
Adam Danz el 1 de Abr. de 2020
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 el 1 de Abr. de 2020
@Adam Danz thank you so much! this is exactly what I need it... I will give it a try :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 31 de Mzo. de 2020

Comentada:

el 1 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by