MATLAB: Grouping elements from a table

table

Hi everyone:
i have a table like this:
Week Open High Low Close
---- ---- ---- --- -----
10 1.2 1.4 1.1 1.2
10 1.5 1.7 1.5 1.6
10 1.4 2.1 1.3 2
10 2.2 2.4 2 2.1
11 2 2.1 2 2
11 2.2 2.5 1.1 1.3
12 1.7 1.9 1.6 1.6
12 1.8 1.9 0.6 1.4
12 0.9 1.4 0.8 1
is there a way to get the first element(open), maximum element(high), minimum element(low) and last element (close) for each week? so the result table would be
Week Open High Low Close
---- ---- ---- --- -----
10 1.2 2.4 1.1 2.1
11 2 2.5 1.1 1.3
12 1.7 1.9 0.6 1
thanks in advance

Best Answer

Assuming your table is T, the following code can generate what you want.
[group, id] = findgroups(T.Week);
func = @(p, q, r, s) [p(1), max(q), min(r), s(end)];
result = splitapply(func, T.Open, T.High, T.Low, T.Close, group)
Tout = array2table([id, result],...
'VariableNames', T.Properties.VariableNames);