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!