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 Center および File ExchangeWhos についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by