How do I sort excel data by Age

3 visualizzazioni (ultimi 30 giorni)
Isabella
Isabella il 2 Mag 2023
Commentato: LeoAiE il 3 Mag 2023
I'm trying to use a loop to seperate the excel data that has ages from 14-90 years old. I need to seperate those numbers into group of young (14-25), medium (26-64) and old (65-90). How would I use a loop to do this?
This is the code I have so far:
syms i
cond = i <= 25;
for i = 1:1:100
if subs(cond, i)
disp (i)
end
end;
The output just consists of matlab listing the numbers 1 to 25.

Risposta accettata

LeoAiE
LeoAiE il 2 Mag 2023
Hi,
Let me know if that's what you looking for!
% Assuming you have an array "ages" containing the age values from the Excel file
ages = [14 20 30 45 65 75]; % Replace this with your actual data from the Excel file
young = [];
medium = [];
old = [];
for i = 1:length(ages)
age = ages(i);
if age >= 14 && age <= 25
young = [young, age];
elseif age >= 26 && age <= 64
medium = [medium, age];
elseif age >= 65 && age <= 90
old = [old, age];
end
end
disp('Young ages:');
Young ages:
disp(young);
14 20
disp('Medium ages:');
Medium ages:
disp(medium);
30 45
disp('Old ages:');
Old ages:
disp(old);
65 75
  2 Commenti
Isabella
Isabella il 2 Mag 2023
Thank you! I just couldn't figure out how to use greater than or less than symbols in Matlab.
Now if I wanted to sort covid results that are written as 1 or 0, would I use the same method? I need to seperate the covid results based on age group, so how many 1s and 0s are in each variable. Could I still use a loop?
LeoAiE
LeoAiE il 3 Mag 2023
I don't have your data but you can use a similar method to separate the COVID results based on the age groups. Assuming you have an array "covid_results" containing the COVID result values (1 or 0) corresponding to the ages in the "ages" array,
% Replace these example data with your actual data from the Excel file
ages = [14 20 30 45 65 75];
covid_results = [1 0 1 1 0 1];
young_covid = [];
medium_covid = [];
old_covid = [];
for i = 1:length(ages)
age = ages(i);
covid_result = covid_results(i);
if age >= 14 && age <= 25
young_covid = [young_covid, covid_result];
elseif age >= 26 && age <= 64
medium_covid = [medium_covid, covid_result];
elseif age >= 65 && age <= 90
old_covid = [old_covid, covid_result];
end
end
young_positives = sum(young_covid);
medium_positives = sum(medium_covid);
old_positives = sum(old_covid);
disp('COVID positives in young age group:');
COVID positives in young age group:
disp(young_positives);
1
disp('COVID positives in medium age group:');
COVID positives in medium age group:
disp(medium_positives);
2
disp('COVID positives in old age group:');
COVID positives in old age group:
disp(old_positives);
1

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by