# How do I compare values in a cell array with values in another array and split the columns accordingly?

1 view (last 30 days)
lil brain on 10 Dec 2022
Commented: lil brain on 10 Dec 2022
Hi,
I have a cell array called newdata. Each cell contains two columns. The first contains seconds and the first column contains values.
I have a second array called split_points_rounded that contains four columns. The columns contain values (seconds) which correspond to cut-off points. Each row in split_points_rounded corresponds to a cell in newdata.
What I want to do is loop through each cell in newdata and split the columns according to the cut-off points in split_points_rounded. The five resulting lists of values should be saved in a new cell array called split_newdata.
For eample, if the values in the first row of split_points_rounded would be 100, 200, 300 and 400 then the column in the first cell in newdata would yield five lists.
1) From the first value in the column to the value 100.
2) From value 100 to value 200.
3) From value 200 to value 300.
4) From value 300 to value 400.
5) From value 400 to the last value in the column.
I feel like this should not be too difficult yet I have trouble wrapping my head around the logic of this. Do I need to write a script first that records the rows of the values in newdata that match the values in split_points_rounded?

Voss on 10 Dec 2022
N = numel(newdata);
split_newdata = cell(1,N);
for ii = 1:N
temp = reshape([newdata{ii}{:}],[],2);
idx = discretize(temp(:,1),[0 split_points_rounded(ii,:) Inf]);
split_newdata{ii} = groupsummary(temp(:,2),idx,@(x){x});
end
% check the result:
split_newdata
split_newdata = 1×5 cell array
{5×1 cell} {5×1 cell} {5×1 cell} {5×1 cell} {5×1 cell}
split_newdata{:}
ans = 5×1 cell array
{127×1 double} { 10×1 double} { 56×1 double} {423×1 double} { 57×1 double}
ans = 5×1 cell array
{ 67×1 double} { 16×1 double} { 63×1 double} {823×1 double} { 13×1 double}
ans = 5×1 cell array
{196×1 double} { 24×1 double} {224×1 double} {112×1 double} {113×1 double}
ans = 5×1 cell array
{228×1 double} { 16×1 double} { 48×1 double} {288×1 double} {225×1 double}
ans = 5×1 cell array
{ 236×1 double} { 48×1 double} { 172×1 double} {1244×1 double} { 209×1 double}
lil brain on 10 Dec 2022
Great stuff! Thanks and I appreciate the speed! :)