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!