How do I compare values in a cell array with values in another array and split the columns accordingly?
1 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Voss
2022-12-10
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{:}
3 个评论
Voss
2022-12-10
编辑:Voss
2022-12-10
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{:}
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!