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

1 view (last 30 days)
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?
Thank you for your help!

Accepted Answer

Voss
Voss on 10 Dec 2022
load newdata
load split_points_rounded
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}
  3 Comments
Voss
Voss on 10 Dec 2022
Edited: Voss on 10 Dec 2022
There are some NaN's so I imagine you want to use the 'omitnan' option in mean.
Here's how you can calculate the means in the same loop you use to split apart the data:
load newdata
load split_points_rounded
N = numel(newdata);
split_newdata = cell(1,N);
split_newdata_mean = 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});
split_newdata_mean{ii} = cellfun(@(x)mean(x,'omitnan'),split_newdata{ii});
end
% check the result:
split_newdata_mean
split_newdata_mean = 1×5 cell array
{5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double}
split_newdata_mean{:}
ans = 5×1
3.0122 0.1663 24.5167 -31.9306 4.7207
ans = 5×1
7.9357 15.5422 28.1723 51.8365 1.0000
ans = 5×1
38.5009 57.6384 35.9230 17.0147 36.2799
ans = 5×1
15.1553 3.0390 42.6316 -15.0934 -16.7131
ans = 5×1
57.9355 51.3927 43.1684 -11.2535 -8.4161

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!