Arranging values in an array
3 次查看(过去 30 天)
显示 更早的评论
I have some values in an array that I need to organize in ranges. lets say I am given different ages of people and I want to arrange them by their age group.
0-10, 11-20, 21-30, 31-40 I have been working on some code but I am uncertain how to finish it. Here is my code. Even just printing how many are in each range is fine with me.
Any help is greatly appreciated!
x = [3, 4, 8, 9, 9, 12, 12, 14, 14, 15, 17, 19, 21, 21, 22, 25, 27, 31, 32, 35, 35, 38];
for i = 1:size(x,1);
if(x <= 10);
elseif(x >=11)&(x <=20);
elseif(x >= 21)&(x <=30);
elseif(x >=31);
else
fprintf 'No values match your conditions\n'
end
end
0 个评论
采纳的回答
Image Analyst
2012-6-27
You can do what you tried if you want to execute some code depending on the value. Or you can use histc(), if that's what you mean by organize.
x = [3, 4, 8, 9, 9, 12, 12, 14, 14, 15, 17, 19, 21, 21, 22, 25, 27, 31, 32, 35, 35, 38]
ranges = [0,11,21,31,41]
counts = histc(x, ranges)
In the command window:
x =
Columns 1 through 14
3 4 8 9 9 12 12 14 14 15 17 19 21 21
Columns 15 through 22
22 25 27 31 32 35 35 38
ranges =
0 11 21 31 41
counts =
5 7 5 5 0
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!