Split and group data in Array

Hello,
I have array of size 2200056*1 and it is plotted below.
I want to split the array based on different steps.
Goal: I want to find index in figure2 based on input signal array (figure1).
In this case, from input of multiple steps(figure 1), the output should be index numbers 1,2,3,4,7,9,10,11
If array consist only one step signal then i can find index easily by taking median(array). But if the array has multiple sequences then how can we achieve it? I think if i can split data in to groups then it would be possible but not sure how to split it?

 Respuesta aceptada

Cris LaPierre
Cris LaPierre el 3 de En. de 2019
Editada: Cris LaPierre el 3 de En. de 2019
Here's one way if you want the values to be rounded to represent what is in your input speed array.
load speed.mat
% Round values to remove perturbations
p10 = floor(log10(speed));
% Set resolution
p10(p10<1)=1; % smallest increment is 10
p10(p10>3)=3; % largest increment is 1000
tmp = speed./10.^p10;
newSpeed = round(tmp).*10.^p10;
% find and remove transitions
idx = find(diff(newSpeed)>0);
idx([inf; diff(idx)]>5000) = [];
newSpeed(ismember(newSpeed,newSpeed(idx)))=[];
% remove tail
idx = find(diff(newSpeed)<0);
newSpeed(idx(1):end)=[];
steps = unique(newSpeed)
steps = 8×1
0
100
600
1000
4000
8000
10000
13000

Más respuestas (2)

Cris LaPierre
Cris LaPierre el 3 de En. de 2019
Editada: Cris LaPierre el 3 de En. de 2019
Use findgroups and splitapply functions.
Here is a simple example provided in the documentation
% Load table containing info for 100 patients
load patients
% Specify groups by gender with findgroups.
G = findgroups(Gender);
% Split Height into groups specified by G.
% Calculate the mean height by gender.
splitapply(@mean,Height,G)
ans = 2×1
65.1509
69.2340

3 comentarios

Cris LaPierre
Cris LaPierre el 3 de En. de 2019
Editada: Cris LaPierre el 3 de En. de 2019
This will give you the value of each step/group.
From there you could use intersect to see which of the input array values are used. For example
% hypothetical step values
A = [1:2:5];
% hypothetical list of possible steps
B = [1:10];
% ib will tell which values of B are found in A
[~,~,ib] = intersect(A,B)
ib =
1
3
5
Cris LaPierre
Cris LaPierre el 3 de En. de 2019
Editada: Cris LaPierre el 3 de En. de 2019
Edit: moving your reply to a comment here:
Luke answered:
Thank you for quick response .
In my case, it wont work like this. Please have a look at attached .mat file and you will know why ?
If I want to group the data in my case then commnad findgroups would be needed more arguments that I cannot specify .
Cris LaPierre
Cris LaPierre el 3 de En. de 2019
Editada: Cris LaPierre el 3 de En. de 2019
This approach will need some modification if your data is not just the step values (e.g. if it also contains values corresponding to the transition between each step). What to do in that scenario will depend on the actual data.
speek10k.png

Iniciar sesión para comentar.

Luke
Luke el 4 de En. de 2019

0 votos

I have come up with different logic as follow which gives same result.
load speed.mat
for i=1:1:length(speed)
speed_filt(i) =round(speed(i),-2);
end
speed_filt = speed_filt';
[n,bin] = hist(speed_filt,unique(speed_filt));
if isempty(n)
inStruct.ind = 0;
else
[~,idx] = sort(-n);
temp1 = n(idx); % count instances
temp1 = temp1';
temp2 = bin(idx); % corresponding values
for i=1:length(temp1)
if temp1(i)> 2000
inStruct.ind(i) = temp2(i);
end
end
end

Categorías

Etiquetas

Preguntada:

el 3 de En. de 2019

Comentada:

el 4 de En. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by