How to convert continuous form of data into discrete steps form?

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 Comments

Given what you've specificed, how does y=8 when x=5?
Well it goes like this:
X is independent variable and Y is dependent varibles, each value of X index is output of Y. so
x= [5,10,15]
y=[3,5,5],
sorry for the calculation mistake.
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
Well, Thank you very much,,
But its says invalid groupping.
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.
One minor point: I recommend against using rng as a variable name, since it is also MATLAB function name.
Why create a new question asking the same thing?
@the cyclist - good point. I renamed the variable to rg.
@Cris, I am getting following error,
Error using groupsummary (line 103)
First argument must be a table or a timetable.
Error in Untitled3 (line 9)
Y = groupsummary(y,x,rg,@mean,"IncludedEdge","right")
Can you try on the data I shared on https://www.mathworks.com/matlabcentral/answers/583223-how-to-convert-continuous-variable-into-discrete-variables?s_tid=mlc_ans_email_view
You can flexibly compute the edges using
rg = 0:5:ceil(X/5)*5;

Sign in to comment.

 Accepted Answer

Try this (uses the data your supplied in the repost). Note that your data in X3 goes to 1.42, not 0.6. It's also unclear if you just want the summarized data, or the full 50,000 rows back. This gives 50,000 rows.
load ChetanBadgujar_DATA.mat
% Define edges of ranges
rg = 0:0.05:0.6;
% Identify groups (
newX3 = discretize(X3,rg,rg(2:end),"IncludedEdge","right");
% Find the max value in each range
Ymean = groupsummary(Y,X3,rg,@mean,"IncludeMissingGroups",false)
newY = discretize(X3,rg,Ymean)

1 Comment

Older versions of MATLAB (before R2018b) require the inputs to groupsummary to be tables. This convert X3 and Y to a table.
load ChetanBadgujar_DATA.mat
data = table(X3,Y);
% Define edges of ranges
rg = 0:0.05:0.6;
% Identify groups (
newX3 = discretize(data.X3,rg,rg(2:end),"IncludedEdge","right");
% Find the max value in each range
Ymean = groupsummary(data,'X3',rg,@mean,'Y',"IncludeMissingGroups",false)
newY = discretize(data.X3,rg,Ymean.fun1_Y)
% Visualize result
plot(X3,Y,'x')
hold on
plot(newX3,newY,'wo')
hold off

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!