Separating age into young and old groups

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!

Antworten (1)

Voss
Voss am 30 Mär. 2022

0 Stimmen

% 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

Kategorien

Mehr zu Software Development Tools finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 30 Mär. 2022

Beantwortet:

am 30 Mär. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by