I have a list of patients, and want to use a for loop to count how many of them are over the age of 12. Very new to loops - any type of help would be appreciated!
patient_ages = [15 8 6 12 16 3 18 12 13 9 8 15 5 2]
over12 = 0
for i=1:length(patient_ages)
if patient_ages > 12
over12 = over12 +1
end
end
return over12
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.

 採用された回答

VBBV
VBBV 2022 年 10 月 18 日

0 投票

patient_ages = [15 8 6 12 16 3 18 12 13 9 8 15 5 2]
over12 = 0
for i=1:length(patient_ages)
if patient_ages(i) > 12
over12 = over12 +1
end
end
over12

4 件のコメント

VBBV
VBBV 2022 年 10 月 18 日
Use the for loop index i for the vector.
Sushi
Sushi 2022 年 10 月 18 日
Thank you so much! :)
VBBV
VBBV 2022 年 10 月 18 日
return is a keyword and usually applicable when the program control is being transferred from within a loop to an external function
Sushi
Sushi 2022 年 10 月 19 日
Understood! Appreciate it!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2021b

タグ

質問済み:

2022 年 10 月 18 日

コメント済み:

2022 年 10 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by