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

采纳的回答

Image Analyst
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
  1 个评论
QuantumReversing
QuantumReversing 2012-6-27
awesome that is exactly what I was looking for. I guess I need to familiarize myself with built in functions of MATLAB instead of trying to manually do it with C code.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by