How do I compare values in a cell array with values in another array and split the columns accordingly?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
lil brain
el 10 de Dic. de 2022
Comentada: lil brain
el 10 de Dic. de 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?
Thank you for your help!
0 comentarios
Respuesta aceptada
Voss
el 10 de Dic. de 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{:}
3 comentarios
Voss
el 10 de Dic. de 2022
Editada: Voss
el 10 de Dic. de 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{:}
Más respuestas (0)
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!