Arranging values in an array

조회 수: 3 (최근 30일)
QuantumReversing
QuantumReversing 2012년 6월 27일
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