# MATLAB: Creating new variable using for loop and if-else condition

for loophomework

Hi,
I wish to divide my age data in column vector into age-groups 10-29, 20-29, 30-39, 40-49, 50-59, etc. using for loop and the if-else statements. here what I have started. but i wonder how to create that new variable age group.
for n=1:length(age)    if age(n)==10 & age(n)<=29        new_age_group1 = age() <what to place here>    elseif age(n)==30 & age(n)<=39        new_age_group2 = age() <what to place here>    end       end

age = 1 : 40; % you were almost close, note: it could be done without loops[new_age_group1, new_age_group2] = deal(cell(40, 1));for n = 1 : numel(age)     if age(n) >= 10 && age(n) <= 29;        new_age_group1{n} = age(n);     elseif age(n) >= 30 && age(n) <= 39        new_age_group2{n} = age(n);    end       endnew_age_group1 = cat(1, new_age_group1{:})new_age_group1 = 20×1    10
11
12
13
14
15
16
17
18
19
new_age_group2 = cat(1, new_age_group2{:})new_age_group2 = 10×1    30
31
32
33
34
35
36
37
38
39
●