Bin edges of discretize function excluding some bins
4 views (last 30 days)
Show older comments
Filip Simjanoski
on 10 Aug 2020
Edited: Cris LaPierre
on 10 Aug 2020
Hello.
mtransf is 100x5000 double where I have some values between -1.3 and 1.3 in each column. I add the mean in row 101 of mtransf:
mtransf(101,:) = mean(mtransf, 1);
I want to discretize the data and make categories in row 102 based on the mean values. With if loops in for loop it works fine but solution is not elegant:
% for i=1:5000
% if (-1.3<=mtransf(101,i)) && (mtransf(101,i)<=(-1.0))
% mtransf(102,i)=1;
% elseif (-1.0<mtransf(101,i)) && (mtransf(101,i)<=(-0.8))
% mtransf(102,i)=2;
% elseif (-0.8<mtransf(101,i)) && (mtransf(101,i)<=(-0.6))
% mtransf(102,i)=3;
% elseif (0.8>=mtransf(101,i)) && (mtransf(101,i)>=0.6)
% mtransf(102,i)=4;
% elseif (1.0>=mtransf(101,i)) && (mtransf(101,i)>0.8)
% mtransf(102,i)=5;
% elseif (1.3>=mtransf(101,i)) && (mtransf(101,i)>1.0)
% mtransf(102,i)=6;
% end
I want to do the same task with discretize, but without the bin between -0.6 and 0.6 :
binEgdes = [-1.3 -1.0 -0.8 -0.6 0.6 0.8 1.0 1.3];
catnames = ["1","2","3","4","5","6","7"];
mtransf(102,:)=discretize(mtransf(101,:),binEgdes,"Categorical",catnames);
Thank you.
2 Comments
Accepted Answer
Cris LaPierre
on 10 Aug 2020
mtransf(102,:) = discretize(mtransf(101,:),binEgdes,"Categorical",catnames);
mtransf(102,mtransf(102,:)==4)=NaN;
5 Comments
Cris LaPierre
on 10 Aug 2020
Edited: Cris LaPierre
on 10 Aug 2020
That works, but be careful. Your X-axis will be doubles again when you create the histrogram this way, meaning your X-axis is continuous. You will see a bar corresponding to X=4, but corresponds to category "5". Same for X=5 and 6.
More Answers (0)
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!