How to convert continuous form of data into discrete steps form?
Afficher commentaires plus anciens
I looking for help to convert countinous form of data into discreate steps form.
Here is the example, please help me to ffind solution.
X = [ 1 3 5 8 10 15 20 25 30 34 38 40 45 50 55 60 65 69 70]
y =[ 1 3 5 4 6 5 8 7 6 7 6 7 6 7 6 7 5 7 0 1]
I want convert X variable in to steps multiple of 5 [0 5 10 15 20 25..] and coresspong values of y betwen these steps should be mean(y). Well I am looking for output in the following format.
x= [5,10,15...]
y=[3,5,5..].
I tried with find(x<5), which gave me indices for calculating the mean (y), but its too laborous method.
Thank you
10 commentaires
Cris LaPierre
le 22 Août 2020
Given what you've specificed, how does y=8 when x=5?
Chetan Badgujar
le 22 Août 2020
Cris LaPierre
le 23 Août 2020
Modifié(e) : Cris LaPierre
le 24 Août 2020
You can use groupsummary.
x = [ 1 3 5 8 10 15 20 25 30 34 38 40 45 50 55 60 65 69 70]';
y =[ 1 3 5 4 6 5 8 7 6 7 6 7 6 7 6 7 5 7 0]';
% Define edges of ranges
rg = 0:5:100;
% Identify groups (
G = discretize(x,rg,"IncludedEdge","right");
% Find the max value in each range
Y = groupsummary(y,x,rg,@mean,"IncludedEdge","right")
X = rg(unique(G+1)) % G+1 is to get the result you show
Chetan Badgujar
le 23 Août 2020
Cris LaPierre
le 23 Août 2020
Modifié(e) : Cris LaPierre
le 23 Août 2020
Sorry, I switched from splitapply to groupsummary and removed the line that creates G. I've updated the code. Try it now. If you are still getting an error message, please share the complete error message. It might also be helpful if you could share you actual data.
Chetan Badgujar
le 24 Août 2020
the cyclist
le 24 Août 2020
One minor point: I recommend against using rng as a variable name, since it is also MATLAB function name.
Cris LaPierre
le 24 Août 2020
Modifié(e) : Cris LaPierre
le 24 Août 2020
Why create a new question asking the same thing?
@the cyclist - good point. I renamed the variable to rg.
Chetan Badgujar
le 24 Août 2020
Adam Danz
le 24 Août 2020
You can flexibly compute the edges using
rg = 0:5:ceil(X/5)*5;
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Tables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
