Finding values from multiple vectors (logical indexing)
10 次查看(过去 30 天)
显示 更早的评论
Hi!
I'm beginner MATLAB user. I'm stuck in a following exercise:
"The following are three vectors representing the age (years), height (cm) and weight (kg) of ten girls. A = [ 12 13 13 14 16 14 15 12 13 12]; H = [143 158 161 150 159 159 150 144 157 137]; W = [ 39 48 49 42 51 51 47 31 43 31]; a) Calculate the average of all vectors (age, height and weight). Put all three means in a column vector named mean_ahw. (Use function mean to calculate the averages.) b) How many girls in the group are taller than 150cm? Put the result in a variable named n150. c) What is the average weight of girls of age 13 or younger? Put the result in a variable named mw13. d) How many girls are taller than the average and at the same time have weight below the average? Put the result in a variable named ntall."
I did a) and b) correct but I don't know how to show the answers correctly in c) and d) when I must find values from 2 different vectors. Can someone help?
Thank you!
2 个评论
Rik
2018-9-19
You indeed need to use logical indexing. That means you use a vector with only true/false values to select a part of another vector of the same length.
So what you need to do for c, is making a vector that contains the information about which values you should pick, then use that to select the weight, and then calculate the average.
Does that help?
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!