Loops to determine what is in certain ranges from data

Hi, so I have a set of data and I'm trying to use loops to determine what data lies between ranges of increments of 10 from 0 (ie 0 <=10) and so on. I've been trying to use for loops for each of the ranges and insert each one into a column vector, but it doesn't seem to work. I'm very new to matlab so any help is greatly appreciated!

 Accepted Answer

Renae: Given that you're new to Matlab, perhaps an explanation of Stephen's solution will help. First he created X as a random dataset to toy with. He used randi to get a 9x4 matrix of random integers in the range of 1 to 99.
The second step is ALMOST what you're trying to do with your data. The floor function simply rounds down, so 24/10 would be 2.4, but floor(24/10) is 2. By Stephen's floor(X/10) solution, anything below 10 is then 0. That's pretty much what you're trying to do, except one minor catch: You said anything less than or equal to 10 should be classified as 0. However, Stephen's solution does not accurately classify values that are exactly equal to 10, 20, 30, or so on. I propose this modified version of Stephen's solution. Rather than rounding down with floor, I round up with ceil and then subtract 1:
X = randi(99,9,4)
X =
25 55 24 42
91 43 12 84
27 64 61 83
76 65 45 26
19 68 46 61
29 63 66 58
10 94 77 54
58 21 35 87
68 71 66 27
>> ceil(X/10)-1
ans =
2 5 2 4
9 4 1 8
2 6 6 8
7 6 4 2
1 6 4 6
2 6 6 5
0 9 7 5
5 2 3 8
6 7 6 2

More Answers (1)

MATLAB code should be beautiful, and using loops is not very beautiful. Perhaps something like this might work for you:
>> X = randi(99,9,4)
X =
24 1 34 79
53 54 83 74
6 21 55 13
75 22 95 82
60 33 89 3
85 10 36 42
98 75 55 73
93 75 35 78
41 54 62 37
>> floor(X/10)
ans =
2 0 3 7
5 5 8 7
0 2 5 1
7 2 9 8
6 3 8 0
8 1 3 4
9 7 5 7
9 7 3 7
4 5 6 3

Asked:

on 12 Feb 2017

Edited:

on 12 Feb 2017

Community Treasure Hunt

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

Start Hunting!