Bin edges of discretize function excluding some bins

4 views (last 30 days)
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.

Accepted Answer

Cris LaPierre
Cris LaPierre on 10 Aug 2020
mtransf(102,:) = discretize(mtransf(101,:),binEgdes,"Categorical",catnames);
mtransf(102,mtransf(102,:)==4)=NaN;
  5 Comments
Filip Simjanoski
Filip Simjanoski on 10 Aug 2020
Okay this solved my problem. In the end I can still add the categories in my matrix:
dis = discretize(mtransf(101,:),binEgdes,"Categorical",catnames);
dis = removecats(dis,"4");
mtransf(102,:)=dis;
histogram(mtransf(102,:))
Thank you for the fast reply.
Cris LaPierre
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!