Grouping data by value

5 views (last 30 days)
Nurul Ain Basirah Zakaria
Answered: Walter Roberson on 8 Feb 2021
Hi. I have 432x1 data. I need to grouping the data.
for example;
-0.989088822111364
-1.16881053159657
-0.687960193675098
0.609028518151482
0.853112207247799
1.09867863373770
0.325581735572437
-0.358624505229866
-0.116184636997357
-1.12249570113502
-0.878711918410750
-0.843582079603111
-0.763107095944730
-0.963235098445007
-1.36135857090159
-1.93513836539865
-1.53179202986538
-1.24445835422832
0.643855927191538
0.506078478817649
1.10211477120999
0.119654919704736
-0.186087379373470
0.208608204079503
0.542441939121425
1.09907331469601
0.595582967612814
0.605674642059506
-0.0429774937224157
How can I assign them to groups such as:
moderate= -1.00 to -1.49
severe= -1.50 to -1.99

Accepted Answer

KSSV
KSSV on 8 Feb 2021
Let x be your data. Create arespective y-value which denotes grouping.
y = zeros(size(x)) ;
% get values -1.00 to -1.49
idx = x >= -1 && x < -1.49 ;
y(idx) = 1 ; % say one stand for moderate
% get values between -1.50 and -1.99
idx = x >= -1.50 && x < -1.99 ;
y(idx) = 2 ; % say two stand for

More Answers (1)

Walter Roberson
Walter Roberson on 8 Feb 2021
discretize() with boundaries and 'categorical' and a list of category names.
data = [
-0.989088822111364
-1.16881053159657
-0.687960193675098
0.609028518151482
0.853112207247799
1.09867863373770
0.325581735572437
-0.358624505229866
-0.116184636997357
-1.12249570113502
-0.878711918410750
-0.843582079603111
-0.763107095944730
-0.963235098445007
-1.36135857090159
-1.93513836539865
-1.53179202986538
-1.24445835422832
0.643855927191538
0.506078478817649
1.10211477120999
0.119654919704736
-0.186087379373470
0.208608204079503
0.542441939121425
1.09907331469601
0.595582967612814
0.605674642059506
-0.0429774937224157];
cats = discretize(data, [-1.99, -1.50, -1, 1, 1.5, 1.99], 'categorical', {'severe', 'moderate', 'nomimal', 'good', 'excellent'})
cats = 29×1 categorical array
nomimal moderate nomimal nomimal nomimal good nomimal nomimal nomimal moderate nomimal nomimal nomimal nomimal moderate severe severe moderate nomimal nomimal good nomimal nomimal nomimal nomimal good nomimal nomimal nomimal

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!