Separating age into young and old groups

7 views (last 30 days)
Ines Bramyan
Ines Bramyan on 30 Mar 2022
Answered: Voss on 30 Mar 2022
Hello,
I am new to matlab/coding and am having difficulty. I was wondering if it is possible to make a script that seperates my age variable into 2 groups with an age limit of 35. I want to create 2 seperate variables one young and one old that extracts from the age variable for example; ages 35 and younger and classifies it as a 'young' variable and older than 35 as and 'old' variable. Would I need to make and if else statement so that it can classify the age variable into potentially 1= young (35 and younger) and 0=old (older than 35) to then make an index for young and old that extracts the 1's into a young variable and the 0's to an old variable?
Thank you!

Answers (1)

Voss
Voss on 30 Mar 2022
% 50 random ages between 1 and 100:
ages = randi(100,1,50)
ages = 1×50
11 26 39 3 49 71 53 67 10 11 98 18 46 92 75 65 50 43 56 61 19 25 94 87 80 70 38 90 59 75
% age limit/threshold for separation:
age_limit = 35;
% logical vector saying whether each age is less than or equal to
% age_limit:
is_young = ages <= age_limit;
disp(is_young);
1 1 0 1 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1
% use logical indexing to separate ages into two new variables:
young_ages = ages(is_young);
old_ages = ages(~is_young);
disp(young_ages);
11 26 3 10 11 18 19 25 29 28 22 20 12 21 29
disp(old_ages);
39 49 71 53 67 98 46 92 75 65 50 43 56 61 94 87 80 70 38 90 59 75 79 69 89 90 87 65 54 87 61 74 37 57 99

Categories

Find more on Software Development Tools 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!