How to convert continuous variable into discrete variables?

I am looking for the help !!
I have 5000 rows of data points for three variables (independent variables-X1,x2,x3 and dependent variables Y). The x1 and x2 are discreate variables in fix steps. However, the X3 variables is countinous ranges from 0-0.6. I want to transfrom this varibles in steps of 0.05 upto 0.6 total in 12 steps and corroespoing value of Y variables should be mean of all values correspond between 0- 0.05 and so.
Well, i tried with find(x3<0.05) and got the index of all values which is less than 0.05. but struggling to compute mean(y) for the indices, mostly they are continous.
Plz help me.

 채택된 답변

Chetan Badgujar
Chetan Badgujar 2020년 8월 24일

0 개 추천

HI Alan,
Thank you for you reply,
I tried but we are losing some information there..I attached the plot comparsion before and after converting the data, discreate data should follow the countinous data form.
load('DATA.mat');
X3(1) = []; % the first values for X3 and Y are NaNs, so they are removed here
Y(1) = [];
x3 = (0:0.05:2)';
Ydiscrete = zeros(length(x3),1);
for i = 2:length(x3)
ix = find(x3(i-1)<=X3<x3(i));
Ydiscrete(i) = mean(Y(ix));
end
plot(x3,Ydiscrete,'o')
hold on
scatter(X3,Y)

댓글 수: 2

Hmm. What about the following:
load('DATA.mat');
X3(1) = []; % the first values for X3 and Y are NaNs, so they are removed here
Y(1) = [];
x3 = (0:0.05:2)';
delta = length(Y)/41;
Ydiscrete = zeros(length(x3),1);
for i = 2:length(x3)
ix = (i-1)*delta:i*delta;
Ydiscrete(i) = mean(Y(ix));
end
subplot(2,1,1)
plot(X3,Y,'.',x3,Ydiscrete,'o')
Y = sort(Y);
X = 1:length(Y);
delta = length(Y)/41;
x = 1:delta:length(Y);
for i = 2:length(x)
ix = (i-1)*delta:i*delta;
Ydiscrete(i) = mean(Y(ix));
end
subplot(2,1,2)
plot(X,Y,'.',x,Ydiscrete,'o')
where the top plot uses X3 and Y, but the bottom plot basically just sorts Y.
:
HI Alan,
Well, I manuuly tried to compute this and explain very well in attached excel sheet Please go through it. The manual calculation sumarize it better way.
so WHat i did,
Step 1) find 0.05 in x and do average y( all values between 0-0.05)
Step 1) find 0.1 in x and do average y( all values between 0.05-0.1)
--- and so on until last value of x
It gives a great result you can check the excel sheet graph.
It would be great help even if You can convert all above steps into code.
Thank you once again.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Alan Stevens
Alan Stevens 2020년 8월 23일
Does the following do what you want?
X3 = 0:0.05:0.6;
Ydiscrete = zeros(length(X3));
for i = 2:length(X3)
ix = find(X3(i-1)<=x3<X3(i));
Ydiscrete(i) = mean(Y(ix));
end
Untested, as I don't have the data!

댓글 수: 1

Hey, Alan Thanks for you reply.
I tired the given code, but it does not work. Well I have attached the data. Could you please test on that?
Thank you..

댓글을 달려면 로그인하십시오.

Alan Stevens
Alan Stevens 2020년 8월 24일
How about this
load('DATA.mat');
X3(1) = []; % the first values for X3 and Y are NaNs, so they are removed here
Y(1) = [];
x3 = (0:0.05:0.6)';
Ydiscrete = zeros(length(x3),1);
for i = 2:length(x3)
ix = find(x3(i-1)<=X3<x3(i));
Ydiscrete(i) = mean(Y(ix));
end
plot(x3,Ydiscrete,'o')
Cris LaPierre
Cris LaPierre 2020년 8월 24일
편집: Cris LaPierre 2020년 8월 26일

0 개 추천

댓글 수: 3

Hi @Cris,
I am using Matlab R2018A and getting following error
Error using groupsummary (line 103)
First argument must be a table or a timetable.
Error in Untitled (line 8)
Ymean = groupsummary(Y,X3,rg,@mean,"IncludeMissingGroups",false)
Yes, you are right I just want to summaries data, convert all rows of 5000 in to whatever discreat steps its coming.
Thanks.
@Cris,
It works well, but when I compare Y with newY.. Code
plot(Y)
hold on
plot(newY)
The NewY doesnot represent the Y.
You can check by plotting both. I feel like there is some data leakage.
Thanks
@ Cris,
Thank you very much.. This is what I was looking for.. Its perfect with manual calculations.
Thank you once again.!!!

댓글을 달려면 로그인하십시오.

질문:

2020년 8월 23일

편집:

2020년 8월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by